JavaScript String.prototype.replaceAll() Method
The replaceAll() method returns a new string with every occurrence of some text replaced.
Description
The replaceAll() method replaces every match of a string or global regular expression. It's clearer than the old trick of split(x).join(y), and saves escaping special characters when you only need a plain string match. A regular expression without the g flag throws a TypeError.
With a string pattern, special regular-expression characters such as . and * are matched literally, so '1.2.3'.replaceAll('.', '-') works without escaping. The replacement can still use the $& pattern for the matched text, and $$ for a literal dollar sign.
As with replace(), the replacement can also be a function that receives each match and returns its replacement. The original string isn't changed. replaceAll() was added in 2021 and is supported in all current browsers.
Syntax
string.replaceAll(pattern, replacement)Parameters
- patternA string, or a regular expression with the
gflag. - replacementThe new text or a function, as for
replace().
Return Value
A new string with every match replaced.
Exceptions
- TypeErrorThrown if the pattern is a regular expression without the
gflag.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.replaceAll() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





