Javascript notes about the String replace Function

Source: Internet
Author: User

I recently consulted javascript and found a function: Copy codeThe Code is as follows: function format (s)
{
Var args = arguments;
Var pattern = new RegExp ("% ([1-" + arguments. length + "])", "g ");
Return String (s). replace (pattern, function (word, index ){
Return args [index];
});
}
// Test
Window. onload = alert (format ("And the % 1 want to know whose % 2 you % 3", "papers", "shirt", "wear "));
// And the papers want to know whose shirt you wear

Such functions may have been seen in shell or java, but the methods implemented in javascript Functions are novel. The novelty is:Copy codeThe Code is as follows: return String (s). replace (pattern, function (word, index ){
Return args [index];
});

However, here the replace usage of the String class is very different from what I usually use. I have previously written such a replace function:Copy codeThe Code is as follows: function myReplace (s)
{
Return String (s). replace (/CJ [0-9] {2}/g, function (word ){
Return word = 'cjjk00 ';
});
}
// Window. onload = alert (myReplace ('cj9080, CJ8976, CJ12919, CJ8765 '); // CJJK0080, CJJK0076, CJJK00919, CJJK0065

When I use replace, if the second parameter is a function, I usually only use the first parameter. I basically didn't think about its second, third, or more parameters, now that someone has used the second parameter, I would like to find out how many parameters are in replace when the second parameter uses function. What is the meaning of each parameter?
Below is the replacement function I wrote myself:Copy codeThe Code is as follows: function myReplaceFtn (s)
{
Return String (s). replace (/CJ [0-9] {2}/g, function (word, index ){
Return word = 'cjjk00 @ '+ index + "@";
});
}
// Window. onload = alert (myReplaceFtn ('cj9080, CJ8976, CJ12919, CJ8765 '); // CJJK00 @ 0 @ 80, CJJK00 @ 7 @ 76, CJJK00 @ 14 @ 919, CJJK00 @ 22 @ 65

I thought that the function (word, index) in the function format should be the index of the string matched by the regular expression (the index of % 1 is 1, the index of % 2 is 2, and the index of % 3 is 3). In the function I wrote, the index of the second parameter is not the index of the matched string, instead, the matched characters are in the original string position. The following is a test:Copy codeThe Code is as follows: function format (s)
{
Var args = arguments;
Var pattern = new RegExp ("% ([1-" + arguments. length + "])", "g ");
Return String (s). replace (pattern, function (word, index ){
Alert ("arguments. length:" + arguments. length); // 4
Return args [index];
});
}
Function myReplaceFtn (s)
{
Return String (s). replace (/CJ [0-9] {2}/g, function (word, index ){
Alert ("arguments. length:" + arguments. length); // 3
Return word = 'cjjk00 @ '+ index + "@";
});
}

In function format, there are four function (word, index) parameters, and three function (word, index) parameters in function myReplaceFtn (s. Why is there such a difference? I did the following test:Copy codeThe Code is as follows: // The following program runs in firefox
Function newformat (s)
{
Var args = arguments;
Var pattern = new RegExp ("% ([1-" + arguments. length + "])", "g ");
Return String (s). replace (pattern, function (word, index ){
Console. log ("arguments. length:" + arguments. length );
For (var I = 0, j = arguments. length; I <j; I ++)
{
Console. log (" newformat" + I + ":" + arguments [I]);
}
Return args [index];
});
}
Function newmyReplace (s)
{
Return String (s). replace (/CJ [0-9] {2}/g, function (word ){
Console. log ("arguments. length:" + arguments. length );
For (var I = 0, j = arguments. length; I <j; I ++)
{
Console. log (" newmyReplace" + I + ":" + arguments [I]);
}
Return word = 'cjjk00 ';
});
}

Result:
Arguments. length: 4
Newformat0: % 1
Newformat1: 1
Newformat2: 8
Newformat3: And the % 1 want to know whose % 2 you % 3
Arguments. length: 4
Newformat0: % 2
Newformat1: 2
Newformat2: 30
Newformat3: And the % 1 want to know whose % 2 you % 3
Arguments. length: 4
Newformat0: % 3
Newformat1: 3
Newformat2: 37
Newformat3: And the % 1 want to know whose % 2 you % 3
Arguments. length: 3
NewmyReplace0: CJ90
NewmyReplace1: 0
NewmyReplace2: CJ9080, CJ8976, CJ12919, CJ8765
Arguments. length: 3
NewmyReplace0: CJ89
NewmyReplace1: 7
NewmyReplace2: CJ9080, CJ8976, CJ12919, CJ8765
Arguments. length: 3
NewmyReplace0: CJ12
NewmyReplace1: 14
NewmyReplace2: CJ9080, CJ8976, CJ12919, CJ8765
Arguments. length: 3
NewmyReplace0: CJ87
NewmyReplace1: 22
NewmyReplace2: CJ9080, CJ8976, CJ12919, CJ8765
The arguments value in the callback function is now clear. The number of arguments should be different from the regular expression we write. In any case, the first parameter is the matched string, the last one is the original string, and the second to last parameter is the start position of the matched string in the original string index, as the second parameter index in format depends on the situation, the newmyReplace parameter I wrote myself does not contain this parameter. The index parameter of format is % [1-4], which is 1-4, but I will write a method to determine it:Copy codeThe Code is as follows: function charFormat (s)
{
Var pattern = new RegExp ("% ([a-d])", "g ");
Return String (s). replace (pattern, function (word, index ){
Switch (index)
{
Case 'A ':
Return 'thisisa ';
Case 'B ':
Return 'thisb ';
Case 'C ':
Return 'thisc ';
Case 'D ':
Return 'thisd ';
Default:
Return 'thisnull ';
}
});
}
Window. onload = console. log (charFormat ("And the % a want to know whose % d you % B", "papers", "shirt", "wear "));
// And the thisisA want to know whose thisisD you thisisB

It can be seen that the String replace is quite powerful, but the skill of the regular expression is not enough. I don't know what other special regular expressions will produce different results. In addition, I don't know who has the original String replace Writing Method in javascript. I hope it can be contributed. I 'd like to study it well.

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.