Favorites

JavaScript String.prototype.replaceAll() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
String.prototype.replaceAll()
JavaScript String Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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 g flag.
  • 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 g flag.

Example

console.log('a-b-c'.replaceAll('-', ' / '));
console.log('1.2.3'.replaceAll('.', ''));
console.log('Hello Hello'.replaceAll(/hello/gi, 'Hi'));

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee