Javascript String.replaceAll() Method

Javascript Update

The replaceAll() method for strings replaces all occurrences of a sub-string with a given replacement string.

This is similar to the replace() method which replaces only the first occurrence. replaceAll() replaces all occurrences.

Until now, developers had to use regular expressions to replace all occurrences of a substring. With replaceAll(), replacing all sub-strings becomes very simple.

let str = "this is a sample string";

// replace each space with dash
str = str.replaceAll(" ", "-");

// "this-is-a-sample-string"
console.log(str);

Browser Compatibility

Tutorials & Resources

April 21, 2020

Comments

Loading Comments