JavaScript String replaceAll() method
replaces ALL occurrences of a substring in a string

replaceAll() method replaces ALL occurrences of a substring in a string.

syntax

replaceAll(searchValue, replaceValue)

searchValue Can be a string or an object with a Symbol.replace method - the typical example being a regular expression.

Any value that doesn't have the Symbol.replace method will be coerced to a string.

If searchValue is a regex, then it must have the global g flag set, or a TypeError is thrown.

replaceValue Can be a string or a function ↴

If it's a string, it will replace ALL occurrences of searchValue with replaceValue string.

If it's a function, it will be invoked for every match and its return value is used as the replacement text.

replaceAll() method returns a new string with the value(s) replaced.

replaceAll() method does not change the original string.

If searchValue is a regular expression then it must have the global g flag set, otherwise TypeError is thrown.

If the searchValue input is left empty, the replaceValue character(s) are inserted between each character (UTF-16 code unit).

If the replaceValue input is left empty, the searchValue character(s) are removed from the string.

syntax description
replace ALL values of searchValue and replace with replaceValue ↴
myString.replaceAll(searchValue, replaceValue)