Thanks to @jonrsharpe, following a link in the documentation he pointed to to react-app-polyfill.

I added the react-app-polyfill node module to my app, and added:

import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

to my index.js file. This will, hopefully, do some polyflling for older browsers, but there's no polyfill for String.prototype.replaceAll(), and I haven't been able to test if it does actually help older browsers use my site that wouldn't otherwise be able to.

Answer from James Gilbert on Stack Overflow
Author: es-shims
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
"aabbcc".replaceAll(/b/g, "."); ("aa..cc"); Polyfill of String.prototype.replaceAll in core-js · es-shims polyfill of String.prototype.replaceAll · Regular expressions guide · String.prototype.replace() String.prototype.match() RegExp.prototype.exec() RegExp.prototype.test() Was this page helpful to you?
Discussions

reactjs - React: Why does Babel not polyfill String.prototype.replaceAll? - Stack Overflow
import 'react-app-polyfill/stable'; doesn't seem to polyfill replaceAll() 2021-07-01T00:12:28.11Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
Script error: Missing polyfill for replaceAll - Meta Stack Exchange
I can confirm that adding a replaceAll polyfill fixes the website. It's a simple fix. No other script errors. More on meta.stackexchange.com
🌐 meta.stackexchange.com
December 14, 2021
String.prototype.replaceAll Polyfill not working
on running the built code, i get items.replaceAll is not a function. Shouldn't this have been properly polyfilled? More on github.com
🌐 github.com
1
1
August 2, 2022
String.prototype.replaceAll polyfill is not included
We use '@vitejs/plugin-legacy' to create legacy build and also include polyfills for our modern browsers. We use String.prototype.replaceAll method in our application, but we still miss its polyfill for the modern browsers, though our browserlist query includes modern browsers which do not ... More on github.com
🌐 github.com
7
March 25, 2022
🌐
Go Make Things
gomakethings.com › how-to-write-your-own-vanilla-js-polyfill
How to write your own vanilla JS polyfill | Go Make Things
Next, we need to create a function for the method we’re polyfilling, and setup some arguments. In this case, the replaceAll() method accepts two arguments: the substring to search for, and the string to replace it with.
🌐
GitHub
github.com › vercel › next.js › discussions › 49474
Node.js 14 deprecation, `replaceAll` polyfill and other polyfills · vercel/next.js · Discussion #49474
Basically, this polyfill will only be loaded if your browser does not include replaceAll - before I started using Next.js this is how I was adding all my polyfills and it would only load the ones required. I wonder if this could be something Next.js would consider at some point instead of having users manage them individually.
Author: vercel
🌐
GitHub
github.com › LinusU › ts-replace-all
GitHub - LinusU/ts-replace-all: String#replaceAll polyfill for TypeScript · GitHub
This package includes the core-js polyfill for String#replaceAll, along with TypeScript typings.
Author: LinusU
🌐
npm
npmjs.com › package › string-replace-all-ponyfill
string-replace-all-ponyfill - npm
December 17, 2020 - The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement.
      » npm install string-replace-all-ponyfill
    
Published: Dec 17, 2020
Version: 1.0.1
Find elsewhere
🌐
Char0n
char0n.github.io › ramda-adjunct › 2.25.0 › replaceAll.js.html
replaceAll.js - Documentation
import { curryN, invoker } from ... = curryN(3, polyfill); export const replaceAllInvoker = invoker(2, 'replaceAll'); /** * Replaces all substring matches in a string with a replacement....
🌐
GitHub
github.com › vitejs › vite › issues › 7449
String.prototype.replaceAll polyfill is not included · Issue #7449 · vitejs/vite
March 25, 2022 - We use String.prototype.replaceAll method in our application, but we still miss its polyfill for the modern browsers, though our browserlist query includes modern browsers which do not have replaceAll implementation · https://stackblitz.com/edit/vitejs-vite-pzezqt ·
Author: vitejs
🌐
npm
npmjs.com › package › string.prototype.replaceall
string.prototype.replaceall - npm
September 12, 2025 - Spec-compliant polyfill for String.prototype.replaceAll ESnext proposal. Latest version: 1.0.11, last published: a year ago. Start using string.prototype.replaceall in your project by running `npm i string.prototype.replaceall`. There are 50 other projects in the npm registry using ...
      » npm install string.prototype.replaceall
    
Published: Sep 12, 2025
Version: 1.0.11
Top answer
1 of 16
5259

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification.


For older/legacy browsers:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

Here is how this answer evolved:

str = str.replace(/abc/g, '');

In response to comment "what's if 'abc' is passed as a variable?":

var find = 'abc';
var re = new RegExp(find, 'g');

str = str.replace(re, '');

In response to Click Upvote's comment, you could simplify it even more:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

Note: Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function (which has changed at least twice since this answer was originally written, so make sure to check the MDN site for potential updates):

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

So in order to make the replaceAll() function above safer, it could be modified to the following if you also include escapeRegExp:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
2 of 16
2537

For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.

Note: In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the String built-in prototype.


Regular Expression Based Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

Split and Join (Functional) Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.

On my Chrome Windows 8 machine, the regular expression based implementation is the fastest, with the split and join implementation being 53% slower. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.

Check out this benchmark running these two implementations against each other.


As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if search contains certain characters which are reserved as special characters in regular expressions. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in Regular Expressions (MDN).

MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as RegExp.escape(str), but alas, it does not exist:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

We could call escapeRegExp within our String.prototype.replaceAll implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).

🌐
Gitbook
olexsyn.gitbook.io › enote › progr › javascript › string › replaceall-polyfill
replaceAll() polyfill - ENote - GitBook
March 13, 2023 - /** * String.prototype.replaceAll() polyfill * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/ * @author Chris Ferdinandi * @license MIT */ if (!String.prototype.replaceAll) { String.prototype.replaceAll = function(str, newStr){ // If a regex pattern if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { return this.replace(str, newStr); } // If a string return this.replace(new RegExp(str, 'g'), newStr); }; } https://vanillajstoolkit.com/polyfills/stringreplaceall/vanillajstoolkit.com ·
Author: es-shims
🌐
GitHub
github.com › babel › babel › issues › 13701
String.prototype.replaceAll not polyfilled · Issue #13701 · babel/babel
August 24, 2021 - 💻 Would you like to work on a fix? How are you using Babel? @babel/cli Input code console.log('123'.includes('2')) console.log('123'.matchAll('1', '2')) console.log('123'.replaceAll('1', '2')) code in Babel REPL Configuration file name ....
Author: babel
🌐
npm
npmjs.com › search
keywords:replaceall - npm search
Spec-compliant polyfill for String.prototype.replaceAll ESnext proposal · string · replaceall · replace · regex · all · es2020 · esnext · javascript · spec · ljharb• 1.0.11 • 9 months ago • 50 dependents • MITpublished version 1.0.11, 9 months ago50 dependents licensed under ...
🌐
X
x.com › _developit › status › 1097302964274892800
Jason Miller 🦊⚛ (@_developit) on X
February 18, 2019 - 🐣 110b String.prototype.replaceAll polyfill: https://t.co/vbPpAQEO3p