JavaScript String replace() Method
- Previous page repeat()
- Next page replaceAll()
- Go up one level JavaScript String Referentiemanual
Definition and Usage
replace()
The method searches for a value or a regular expression in the string.
replace()
The method returns a new string with the replaced value.
replace()
The method does not change the original string.
Tip:If you replace the value, only the first instance will be replaced. To replace all instances, use the one with g
Regular expressions for modifier sets.
See also:
Instance
Example 1
Replace Microsoft:
let text = "Visit Microsoft!"; let result = text.replace("Microsoft", "W3School");
Example 2
Global replacement:
Return the function that returns the replacement text: let result = text.replace(/blue/g, "red");
Example 3
Global, case-insensitive replacement:
Return the function that returns the replacement text: let result = text.replace(/blue/gi, "red");
例子 4
Example 4
Return the function that returns 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();
try it yourself
stringsyntaxregexp.replace( replacement,
)
) | type |
---|---|
regexp |
parameters required. The value to be searched or the regular expression. This parameter specifies the RegExp object to be replaced. |
replacement |
If the parameter is a string, it is used as the direct quantity text pattern to be retrieved. required. String. |
technical details
specifies the replacement text or the function that generates the replacement text. | type |
---|---|
indicates | description |
a new string with the specified value replaced.
technical details
return value replacement returns a new string, which is a new string regexp replaced
the first match or all matches after the match.
indicates string string replace()
in string method performs a search and replace operation. It will regexp finds the substring that matches replacement matches the substring, then regexp to replace these substrings. If replace()
has the global flag g, then
replacement the method will replace all matching substrings. Otherwise, it only replaces the first matching substring. If replacement can be a string or a function. If it is a string, then each match will be replaced by the string. However $
characters have specific meanings. As shown in the table below, it explains that the string obtained from the pattern matching will be used for replacement.
characters | replacement text |
---|---|
$1, $2, ..., $99 | with regexp matches the first 99 subexpressions from the 1st to the 99th. |
$& | with regexp the matched substring. |
$` | text located to the left of the matched substring. |
$' | text located to the right of the matched substring. |
$$ | direct quantity symbols. |
Note:ECMAScript v3 specifies thatreplace()
method parameters replacement can be a function instead of a string. In this case, the function is called for each match, and the string it returns is used as the replacement text. The first parameter of the function is the string of the matching pattern. The following parameters are the strings that match the subexpressions in the pattern, and there can be 0 or more such parameters. The next parameter is an integer that declares the position of the match in string at the position in the string. The last parameter is string itself.
browser support
replace()
It is an ECMAScript1 (ES1) feature.
All browsers fully support ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous page repeat()
- Next page replaceAll()
- Go up one level JavaScript String Referentiemanual