text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
Try it Yourself »
text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");
Try it Yourself »
More example below.
The 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.
The replaceAll()
method does not work in Internet Explorer.
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:
string.replaceAll(
searchValue, newValue)
Parameter | Description |
searchValue | Required. The value, or regular expression, to search for. |
newValue | Required. The new value (to replace with). This parameter can be a JavaScript function. |
Type | Description |
A string | A new string where the search values has been replaced. |
A global, case-insensitive replacement:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replaceAll(/blue/gi, "red");
Try it Yourself »
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();
});
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!