Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2020.
The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
Can be a string or an object with a Symbol.replace method — the typical example being a regular expression. Any value that doesn't have the Symbol.replace method will be coerced to a string.
If pattern is a regex, then it must have the global (g) flag set, or a TypeError is thrown.
replacementCan be a string or a function. The replacement has the same semantics as that of String.prototype.replace().
A new string, with all matches of a pattern replaced by a replacement.
Thrown if the pattern is a regex that does not have the global (g) flag set (its flags property does not contain "g").
This method does not mutate the string value it's called on. It returns a new string.
Unlike replace(), this method replaces all occurrences of a string, not just the first one. While it is also possible to use replace() with a global regex dynamically constructed with RegExp() to replace all instances of a string, this can have unintended consequences if the string contains special characters that have meaning in regular expressions (which might happen if the replacement string comes from user input). While you can mitigate this case using RegExp.escape() to make the regular expression string into a literal pattern, it is simpler to pass the string to replaceAll() directly, without converting it to a regex.
If pattern is an object with a Symbol.replace method (including RegExp objects), that method is called with the target string and replacement as arguments. Its return value becomes the return value of replaceAll(). In this case the behavior of replaceAll() is entirely encoded by the [Symbol.replace]() method, and therefore will have the same result as replace() (apart from the extra input validation that the regex is global).
If the pattern is an empty string, the replacement will be inserted in between every UTF-16 code unit, similar to split() behavior.
For more information about how regex properties (especially the sticky flag) interact with replaceAll(), see RegExp.prototype[Symbol.replace]().
When using a regular expression search value, it must be global. This won't work:
This will work:
| ECMAScript® 2027 Language Specification # sec-string.prototype.replaceall |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Apr 12, 2026 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.