Regular expression replace () function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.