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);
Comments