How do I replace all occurrences of a string in JavaScript?

Created by Sean BrightLinked to 10.7m issues across 80 teams

tl;dr

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

For older browsers, you can use the following utility function:

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