Replace Microsoft:
let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "W3Schools");
A global replacement:
let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red");
More examples below.
DescriptionThe replace()
method searches a string for a value or a regular expression.
The replace()
method returns a new string with the value(s) replaced.
The replace()
method does not change the original string.
If you replace a value, only the first instance will be replaced. To replace all instances, use a regular expression with the g modifier set.
Read more about regular expressions in our:
See Also:The replaceAll() Method - replaces all matches
Syntaxstring.replace(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.replace(/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.replace(/blue|house|car/gi, function (x) {
return x.toUpperCase();
});
replace()
is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome Edge Firefox Safari Opera IE Yes Yes Yes Yes Yes YesTrack 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