Example of JavaScript String. replace function parameters

Source: Internet
Author: User
Tags dot net

Email: longsu2010 at yeah dot net
The function signature of the replace function of js String is as follows:
Replace (match/* string OR regular expression */, replacement/* string OR function */)
The function is to replace match in the source string with replacement and return the replaced string.
If the first parameter is a string, there is nothing to say, but remember that at this time, only the match (first time) function is executed after the source string is replaced.

Therefore, the first parameter is usually a regular expression, for example:
Replace (/a/g, "B") // replace all a in the source string with B.

The second parameter can be a string that contains the grouping of the first parameter regular expression. For example:
Replace (/(a) {2, 2}/g, "$ 1b") // replace all aa in the source string with AB.

If the second parameter is a function, what is the function parameter? Example:Copy codeThe Code is as follows: "bbabc". replace (/(a) (B)/g, function (){
Console. log (arguments)
});

The parameters are as follows:
1. The characters matching the entire regular expression.
2. Content matched by the first group and content matched by the second group ...... And so on until the last group.
3. The subscript (position) of the match in the source string ).
4. Source string
So the output of the example is

["AB", "a", "B", 2, "bbabc"]

The return value of the second parameter will be replaced with the source string, because if the js function does not return a value, the function will be called to get undefined, therefore, if the second parameter has no returned value, undefined will be replaced with the source string.
If the first parameter is a string and the second parameter is a function, the first parameter is a regular expression without grouping, so that the second parameter can be determined.
There are some improper expressions in this article, such as "replacing it with the source character string" (the source character string only acts as the role of the template and does not actually change, and the string is an unchangeable variable ), I hope it will not mislead you.

Related Article

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.