Skip to main content Accessibility Feedback

Strings

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.


Find this useful? You can support my work by purchasing an annual membership.