Example 1 — Replace Every Literal Match
A string pattern swaps every occurrence — no regex required.
const text = "hello world, world";
console.log(text.replaceAll("world", "JS"));
// "hello JS, JS"
console.log("aabbcc".replaceAll("b", "."));
// "aa..cc" How It Works
- Pass a plain string.
"world"is treated as literal text. - Swap every hit. Both
"world"occurrences become"JS". - Works for characters too. Every
"b"in"aabbcc"becomes".". - Original unchanged. Always use the returned string.

