Return a string where "Microsoft" is replaced with "W3Schools":
The result of res will be:
More "Try it Yourself" examples below.
The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.
Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.
This method does not change the original string.
Method | |||||
---|---|---|---|---|---|
replace() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
searchvalue | Required. The value, or regular expression, that will be replaced by the new value |
newvalue | Required. The value to replace the searchvalue with |
Return Value: | A new String, where the specified value(s) has been replaced by the new value |
---|---|
JavaScript Version: | 1.2 |
Perform a global replacement:
The result of res will be:
Perform a global, case-insensitive replacement:
The result of res will be: