text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");
More example below.
DescriptionThe replaceAll()
method searches a string for a value or a regular expression.
The replaceAll()
method returns a new string with all values replaced.
The replaceAll()
method does not change the original string.
The replaceAll()
method was introduced in JavaScript 2021.
If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
Read more about regular expressions in our:
Syntaxstring.replaceAll(searchValue, newValue)
Parameters Parameter Description searchValue Required.A global, case-insensitive replacement:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replaceAll(/blue/gi, "red");
A function to return the replacement text:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replaceAll(/blue|house|car/gi, function (x) {
return x.toUpperCase();
});
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4