JavaScript
String Methods
replace / replaceAll
เรียนรู้ replace() และ replaceAll() — สอง method สำหรับแทนที่ข้อความใน string ตั้งแต่ replace() ที่เปลี่ยนเฉพาะ match แรก, replaceAll() ที่เปลี่ยนทุก match โดยไม่ต้องวน loop, การเทียบความแตกต่างในตาราง, การใช้ในงานจริงเช่นทำความสะอาดเบอร์โทรและสร้าง URL slug, ไปจนถึงข้อควรระวังเรื่อง immutable, case sensitivity, และกับดัก replace() ที่เปลี่ยนแค่ตัวแรก ผ่าน Lab 5 ข้อ
`replace()` คืออะไร — แทนที่ข้อความที่เจอครั้งแรก
`replace()` เป็น method บน string ใช้ค้นหาข้อความ (searchValue) และแทนที่ด้วยข้อความใหม่ (newValue) — แต่จะแทนที่เฉพาะ**ตำแหน่งแรกที่เจอ**เท่านั้น สิ่งที่ต้องรู้: • `replace(searchValue, newValue)` — ค้นหา `searchValue` ตัวแรกที่เจอ แล้วแทนที่ด้วย `newValue` • แทนที่เฉพาะครั้งแรก — ถ้ามี `searchValue` หลายที่ จะเปลี่ยนแค่ที่เดียว • คืนค่าเป็น string ใหม่ — string ต้นฉบับไม่เปลี่ยน (immutable) • `searchValue` เป็น string ธรรมดาได้ — ไม่จำเป็นต้องเป็น regex (ในบทนี้เราใช้ string ก่อน) • ถ้าไม่เจอ `searchValue` — คืนค่า string เดิม ไม่เกิด error
const text = "hello world hello";
// replace() — แทนที่ "hello" ตัวแรกที่เจอ ด้วย "hi"
const result = text.replace("hello", "hi");
console.log(text); // "hello world hello" — ต้นฉบับไม่เปลี่ยน
console.log(result); // "hi world hello" — เฉพาะ hello ตัวแรกถูกแทนที่
// === สังเกต: hello ตัวที่สองยังอยู่เหมือนเดิม
// เพราะ replace() เปลี่ยนเฉพาะ match แรกconst text = "hello world";
// ค้นหา "xyz" — ซึ่งไม่มีใน string
const result = text.replace("xyz", "abc");
console.log(result); // "hello world" — คืนค่าเดิม ไม่เปลี่ยน ไม่ error
// === เปรียบเทียบกับการเจอ match
console.log(text.replace("hello", "hi")); // "hi world" — เจอ match → แทนที่- `replace(searchValue, newValue)` — แทนที่เฉพาะ `searchValue` ตัวแรกที่เจอ
- คืน string ใหม่ — ต้นฉบับไม่เปลี่ยน
- ถ้าไม่เจอ `searchValue` — คืน string เดิม ไม่เกิด error
- ใช้กับ string ธรรมดาได้ — ไม่ต้องรู้ regex เพื่อเริ่มต้น
`replaceAll()` — แทนที่ทุกที่ที่เจอในครั้งเดียว
`replaceAll()` ถูกเพิ่มเข้ามาใน JavaScript รุ่นใหม่ (ES2021) — ใช้แทนที่**ทุกตำแหน่ง**ที่เจอ `searchValue` โดยไม่ต้องวน loop หรือใช้ regex แบบ `/g` สิ่งที่ต้องรู้: • `replaceAll(searchValue, newValue)` — เหมือน `replace()` แต่เปลี่ยนทุก match • syntax เหมือน `replace()` ทุกประการ — ต่างกันแค่จำนวน match ที่ถูกแทนที่ • คืน string ใหม่ — ต้นฉบับไม่เปลี่ยน • **ต้องการให้ `searchValue` เป็น string** — ใช้ string ธรรมดาก็พอ (ไม่ต้องใช้ regex แบบ `/g` เหมือนสมัยก่อน) • ถ้าไม่เจอ `searchValue` — คืน string เดิม ไม่ error
const text = "hello world hello hello";
// replaceAll() — แทนที่ "hello" ทุกตัว ด้วย "hi"
const result = text.replaceAll("hello", "hi");
console.log(text); // "hello world hello hello" — ต้นฉบับไม่เปลี่ยน
console.log(result); // "hi world hi hi" — hello ทุกตัวถูกแทนที่ ✓
// === เปรียบเทียบกับ replace() ตรง ๆ
const replaceOnly = text.replace("hello", "hi");
console.log(replaceOnly); // "hi world hello hello" — แค่ตัวแรก// === แทนที่ตัวอักษรที่ซ้ำกัน
const spaced = "a-b-c-d-e";
console.log(spaced.replaceAll("-", " ")); // "a b c d e"
// === แทนที่ช่องว่างทั้งหมด
const messy = "one two three";
console.log(messy.replaceAll(" ", " ")); // "one two three"
// === แทนที่ substring ยาว ๆ
const html = "<div><div><div>";
console.log(html.replaceAll("<div>", "<section>"));
// "<section><section><section>"
// === ถ้าไม่เจอ searchValue — ไม่ error ไม่เปลี่ยน
console.log("hello".replaceAll("xyz", "abc")); // "hello"- `replaceAll(searchValue, newValue)` — แทนที่ทุก match ใน string
- ต่างจาก `replace()` แค่เปลี่ยนทุกที่ — syntax เหมือนกันทุกประการ
- ใช้ string ธรรมดาได้ — ไม่ต้องใช้ regex `/g` แบบสมัยก่อน
- ถ้าไม่เจอ `searchValue` — คืน string เดิม ไม่ error
เปรียบเทียบ `replace()` vs `replaceAll()` — ดูความต่างให้ชัด
ความต่างสำคัญระหว่าง `replace()` และ `replaceAll()` อยู่ที่จำนวน match ที่ถูกแทนที่ — `replace()` เปลี่ยนแค่ตัวแรก, `replaceAll()` เปลี่ยนทุกตัว เลือกใช้ตามโจทย์: • **เปลี่ยนแค่ตำแหน่งแรก** → `replace()` • **เปลี่ยนทุกตำแหน่ง** → `replaceAll()`
| พฤติกรรม | `replace()` | `replaceAll()` | ตัวอย่าง |
|---|---|---|---|
| แทนที่ match แรก | ✅ ใช่ — แค่ตัวแรก | ✅ ใช่ — ทุกตัว (รวมตัวแรกด้วย) | "a a a".replace("a","b") → "b a a" "a a a".replaceAll("a","b") → "b b b" |
| แทนที่ทุก match | ❌ ไม่ — ต้องใช้ loop หรือ regex | ✅ ใช่ — เปลี่ยนให้หมดในคำสั่งเดียว | "a a a".replace("a","b") → "b a a" "a a a".replaceAll("a","b") → "b b b" |
| ไม่เจอ searchValue | คืน string เดิม | คืน string เดิม | เหมือนกันทั้งคู่ — ไม่ error |
| ต้นฉบับเปลี่ยนไหม | ❌ ไม่เปลี่ยน (immutable) | ❌ ไม่เปลี่ยน (immutable) | เหมือนกันทั้งคู่ — คืน string ใหม่เสมอ |
| แนะนำให้ใช้เมื่อไหร่ | ต้องการเปลี่ยนแค่ครั้งแรกโดยตั้งใจ | ต้องการเปลี่ยนทุก match | เลือกตามจำนวน match ที่ต้องการเปลี่ยน |
const text = "cat dog cat bird cat";
// replace() — เปลี่ยนเฉพาะ "cat" ตัวแรก
const replaceFirst = text.replace("cat", "panda");
console.log(replaceFirst); // "panda dog cat bird cat"
// replaceAll() — เปลี่ยน "cat" ทุกตัว
const replaceAll = text.replaceAll("cat", "panda");
console.log(replaceAll); // "panda dog panda bird panda"
// === ต้นฉบับไม่เปลี่ยนทั้งคู่
console.log(text); // "cat dog cat bird cat"
// === ใช้แทนตัวคั่นใน path
const path = "home/about/contact";
console.log(path.replace("/", " > ")); // "home > about/contact"
console.log(path.replaceAll("/", " > ")); // "home > about > contact"- **เปลี่ยนแค่ตำแหน่งแรก** → ใช้ `replace()` — เช่น แก้ตัวสะกดคำแรกที่เจอ
- **เปลี่ยนทุกตำแหน่ง** → ใช้ `replaceAll()` — เช่น ลบช่องว่างทั้งหมด, เปลี่ยนตัวคั่นทุกที่
- **syntax เหมือนกัน** — แค่เปลี่ยนชื่อ method จาก `replace` เป็น `replaceAll`
- **ไม่ต้องใช้ regex** — ยุคใหม่ใช้ `replaceAll()` กับ string ธรรมดาได้เลย
ใช้ `replace()` และ `replaceAll()` ในสถานการณ์จริง
`replace()` และ `replaceAll()` มี use-case จริงที่ใช้บ่อยมากในงาน frontend และ backend — ตั้งแต่การทำความสะอาดข้อมูล ไปจนถึงการแปลงรูปแบบข้อความ
// === ผู้ใช้กรอกเบอร์โทรมาหลายรูปแบบ
const phone1 = "081-234-5678";
const phone2 = "081 234 5678";
const phone3 = " 0812345678 ";
// replaceAll() + trim() — ทำความสะอาดให้เหลือแค่ตัวเลข
const clean1 = phone1.replaceAll("-", "").trim();
const clean2 = phone2.replaceAll(" ", "").trim();
const clean3 = phone3.trim();
console.log(clean1); // "0812345678"
console.log(clean2); // "0812345678"
console.log(clean3); // "0812345678"
// === ใช้ replaceAll() chain — ลบทั้งขีดและ space
const messy = "081 - 234 - 5678";
const clean = messy.replaceAll("-", "").replaceAll(" ", "");
console.log(clean); // "0812345678"// === สร้าง slug จากชื่อบทความ
const title = " Learn JavaScript Basics ";
// trim() → replaceAll(" ", "-") → toLowerCase()
const slug = title.trim().replaceAll(" ", "-").toLowerCase();
console.log(slug); // "learn-javascript-basics"
// === ใช้ซ้ำกับหลายหัวข้อ
function toSlug(text) {
return text.trim().replaceAll(" ", "-").toLowerCase();
}
console.log(toSlug(" Hello World ")); // "hello-world"
console.log(toSlug("My First Post")); // "my-first-post"
console.log(toSlug(" What Is Node JS ")); // "what-is-node-js"ข้อควรระวังและข้อผิดพลาดที่พบบ่อย
ถึงแม้ `replace()` และ `replaceAll()` จะใช้ไม่ยาก — แต่มีจุดที่คนมักพลาดบ่อย โดยเฉพาะเรื่องจำนวน match, immutable behavior, และ case sensitivity
// === คนส่วนใหญ่คาดว่า replace() จะเปลี่ยนทุกตัว — แต่ไม่ใช่!
const text = "a a a a a";
// ❌ ผิดคาด — replace() เปลี่ยนแค่ตัวแรก
const wrong = text.replace("a", "b");
console.log(wrong); // "b a a a a" — แค่ตัวแรก!
// ✅ ถูก — ใช้ replaceAll() เพื่อเปลี่ยนทุกตัว
const correct = text.replaceAll("a", "b");
console.log(correct); // "b b b b b"// === replace() ไม่เปลี่ยน string ต้นฉบับ — ต้องรับค่าใหม่
let name = "Mali";
// ❌ ผิด — คิดว่า name จะเปลี่ยน
name.replace("M", "P");
console.log(name); // "Mali" — ยังเหมือนเดิม!
// ✅ ถูก — ต้อง assign กลับ
name = name.replace("M", "P");
console.log(name); // "Pali"
// === ใช้ chain method ได้ — เพราะแต่ละขั้นคืน string ใหม่
const raw = " HELLO WORLD ";
const result = raw.trim().replaceAll(" ", "-").toLowerCase();
console.log(result); // "hello-world"// === replace() มอง "Hello" กับ "hello" เป็นคนละคำ
const text = "Hello hello HELLO";
console.log(text.replace("hello", "hi")); // "Hello hi HELLO"
console.log(text.replace("Hello", "hi")); // "hi hello HELLO"
console.log(text.replaceAll("hello", "hi")); // "Hello hi HELLO"
// === ถ้าอยากเปลี่ยนทุกแบบ — ใช้ toLowerCase() ก่อน
const unified = text.toLowerCase().replaceAll("hello", "hi");
console.log(unified); // "hi hi hi"- **replace() เปลี่ยนแค่ตัวแรกเสมอ** — อยากเปลี่ยนทุกตัวให้ใช้ `replaceAll()`
- **string เป็น immutable** — ต้อง `str = str.replace(...)` ห้ามลืม assign กลับ
- **case sensitive** — `'Hello'` กับ `'hello'` ถือว่าต่างกัน
- **replaceAll() chain ได้** — `str.replaceAll('a','b').replaceAll('c','d')`
- **ไม่ต้องใช้ regex เพื่อเริ่มต้น** — string ธรรมดาก็พอสำหรับงานทั่วไป