String.prototype.replaceAll()
Replace all instances of a string inside another string, without the need for a regular expression. The String.replaceAll()
method accepts two arguments: the string to find, and the string to replace it with.
// Awkwardly worded, but roll with it
let wizards = 'Of all the wizards in Lord of the Rings, Radagast is my favorite of the wizards.';
// returns "Of all the sorcerers in Lord of the Rings, Radagast is my favorite of the sorcerers."
let sorcerers = wizards.replaceAll('wizards', 'sorcerers');
The String.replaceAll()
method works in all modern browsers, with the exception of the Samsung Internet mobile browser. You can add support with a polyfill.
⏰ Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.
Find this useful? You can support my work by purchasing an annual membership.