JavaScript string.replace function

Source: Internet
Author: User

The code is as follows Copy Code
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 function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the 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:
  
   
   
The code is as follows Copy Code
function Replacestr (s) {

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:
  
   
   
The code is as follows Copy Code
function Replacestr (s) {

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 matching 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.

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.