To replace all occurrences of a string in JavaScript, use str.replaceAll(search, replacement), which is available in all current browsers and in Node.js 15 and later. The older replace() method with a string argument changes only the first match, so before replaceAll() people used a global regular expression instead.
This guide shows both approaches, the special replacement patterns, and the pitfalls with regular expression characters.
replaceAll() returns a new string with every match replaced. The original string is not changed, because strings are immutable.
A string pattern is matched literally, so special characters need no escaping:
replace() with a string pattern replaces just the first occurrence.
To replace everything with replace(), pass a regular expression with the g (global) flag:
replaceAll() is clearer and avoids the escaping problem described next.
replaceAll() also accepts a regex, but it must have the g flag. Without it, JavaScript throws a TypeError.
Use a regex when you need patterns, case-insensitivity (/hello/gi) or character classes. When the text to find comes from user input, prefer the string form of replaceAll(); if you must build a regex from it, escape it first:
The replacement string supports special patterns.
| Pattern | Inserts |
|---|---|
$& | the matched text |
$1, $2 | captured groups (regex only) |
$<name> | a named group (regex only) |
$` | text before the match |
$' | text after the match |
$$ | a literal dollar sign |
For dynamic replacements, pass a function. It receives the match and any groups and returns the new text:
A function replacement also avoids accidental $ patterns when the replacement text is user data, since its return value is used literally.
For a case-insensitive replacement use a regex with gi:
If you must support very old runtimes without replaceAll(), str.split(search).join(replacement) replaces every literal occurrence without a regex, and it is still a handy trick:
Replacing inside the result of another replace can create new matches. Each call works on the string it receives, so chain them deliberately.
An empty search string inserts the replacement between every code unit, which is rarely what you want:
Finally, do not use replaceAll() to remove characters in a hot loop over huge strings if one regex pass can do the job. A single replace(/[aeiou]/g, "") scans the text once, while several chained calls scan it several times.
With a string pattern, replace() changes only the first match and replaceAll() changes every match. With a regex, replace() needs the g flag to change all matches, while replaceAll() requires it.
No. Strings are immutable, so it returns a new string and you must use the return value.
Yes when you pass a string. Use a regular expression with the i and g flags to ignore case.
You passed a regular expression without the g flag. Add g or switch to a string pattern.
Use replaceAll() to replace every occurrence of a string, and replace() with a /.../g regex when you need patterns on older runtimes. String patterns are literal, regex patterns need the g flag in replaceAll(), and replacement functions give you full control without $ surprises.