Also say the Replace function of string class in JavaScript _javascript technique

Source: Internet
Author: User
The parameter descriptions for the callback function are also accurate:
The first argument is the string to match to, the last is the original string, and the penultimate argument is the starting bit of the string that matches to the original string index.
But I'm curious, what is the argument between the second and third? In fact, W3school has given the answer:
Copy Code code as follows:

The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression. Its syntax is:
Stringobject.replace (regexp/substr,replacement)
Replacement can be either a string or a function. If it is a string, then each match is replaced by a string.
ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function rather than a string. In this case, each match calls the letter
Number, the string it returns will be used as the replacement text. The first parameter of the function is a string that matches the pattern. The next argument is the one that matches the subexpression in the pattern.
String, you can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. Last parameter
is Stringobject itself.

Obviously, the argument between the second and last third of the replacement function is "a string that matches the subexpression in the pattern", which is determined by the number of the child expression.
Accordingly, we give two examples to compare the description:
Example 1:
String: "CJ9080"
Match mode is:/cj[0-9]{2}/g (no subexpression)
Expected results:
The replacement function has 3 parameters, respectively:
"0" "CJ90"
"1" 0
"2" "CJ9080"
Test code:
Copy Code code as follows:

function Replacestr (s) {
Return S.replace (/cj[0-9]{2}/g,
function () {
for (var i = 0, len = arguments.length i < len; i++) {
Console.info ("Argument" + i + ":" + arguments[i]);
}
});
};

Run Result:


Example 2:
String: "CJ9080"
Match mode is:/((CJ) ([0-9]{2}))/g (with 3 subexpression: (Cj[0-9]{2}), (CJ), ([0-9]{2})
Expected results:
The replacement function has 6 parameters, respectively:
"0" "CJ90"
"1" "CJ90"
"2" "CJ"
"3" "90"
"4" 0
"5" "CJ9080"
Test code:
Copy Code code as follows:

function Replacestr (s) {
Return S.replace ((CJ) ([0-9]{2}))/g,
function () {
for (var i = 0, len = arguments.length i < len; i++) {
Console.info ("Argument" + i + ":" + arguments[i]);
}
});
};

Run Result:


Obviously, the results of the two test examples are the same as expected. Note that when the replacement of the Replace function is a function, the arguments for this function are indeed as W3school said:

"0": a string that matches the pattern;
"1-(length-3)": A string, 0 or more, that matches a subexpression in the pattern;
"Length-2": matches the string at the beginning of the index of the original string, starting at 0;
"Length-1": the original string.
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.