Mutating strings in JavaScript is a core necessity in being able to provide truly dynamic content. So being able to do it efficiently is important. With that, luckily we have a pretty cool built-in method on the String prototype in replaceAll.

With this method, we can globally replace all matching patterns with a replacement in your string. It can handle patterns that are strings or a Regex. Let’s look at some code!

const testString = 'The test dog is a test to replace all dog instances'.

testString.replaceAll("dog", "chicken");
//output:
// "The test chicken is a test to replace all chicken instances."

Simple!