The regular expression replace () function:
This function substitutes a specified string for a substring that matches a literal expression in a string.
The return value is a new, replaced string.
Only the relevant operations of the regular expression are described here, and the other substitution operations can refer to the section of the Replace () method of the JavaScript string object.
Syntax structure:
Stringobject.replace (regexp,replacement)
The parameter list is as follows:
RegExp required. The RegExp object.
Replacement required. A string value. A function that specifies replacement text or generates alternate text.
Browser support:
This method is supported by IE browser.
This method is supported by the Firefox browser.
Google Chrome supports this method.
Instance code:
var str= "I love Antzone"; var reg=/a (?: \ W) +e/; Console.log (Str.replace (Reg,"Girls"));
The code above can replace a substring in a string with a "girls" that can be matched by a regular expression.
Special Note:
Replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string.
If the $ character in replacement has a specific meaning, as shown in the following table, it indicates that the resulting string from the pattern match will be used for substitution.
$, $ 、...、 $9 matches the 1th to 9th sub-expression in RegExp.
$& the substring that matches the regexp.
$$ Direct volume symbol.
The text in the $ ' input string before lastmatch.
The text after lastmatch in the $ ' input string.
Instance code:
var str= "Ant,zone"var newstr=str.replace (/(\w+), (\w+)/, "$ $"); Console.log (newstr );
The original address is: Regular expression replace () function
Regular expression replace () function