Javascript notes: Something about the string replace Function

Source: Internet
Author: User

ReinforcementJavascriptThe basic knowledge is intended for future researchJqueryThe source code paves the way.

I recently reviewedJavascriptData, found a function:

 

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

 

This functionShellOrJavaYou may have seen it before, but inJavascriptFunction implementation methods are novel. The novelty is:

Return string (s). Replace (pattern, function (word, index) {return ARGs [Index] ;});

 

But hereStringClassReplaceThe usage is very different from what I usually use.ReplaceFunction:

 

 

 
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

I am usingReplaceIf the second parameter isFunctionI usually only use the first parameter. I basically didn't think about its second, third, or more parameters. Now, if someone uses the second parameter, I 'd like to explore it.ReplaceThe second parameter is usedFunctionHow many parameters are there, and what is the meaning of each parameter?

Below is the replacement function I wrote myself:

 

 
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, FunctionFormatInFunction (word, index), I think it should be the index of the string matched by the regular expression (% 1The index is1,% 2The index is2,% 3The index is3), And the second parameter in the function I wroteIndexIt is not the index of the matched string, but the matched character in the original string. The following is a test:

 

 

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 + "@";});}

FunctionFormatInsideFunction (word, index)Parameters include:4And functionsMyreplaceftn (s)InsideFunction (word, index)Parameters include:3. Why is there such a difference? I did the following test:

 

 

// The following Program Run 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 ("marked newmyreplace" + I + ":" + arguments [I]);} return word = 'cjjk00';}) ;}result: arguments. length: 4 Mark newformat0: % 1 mark newformat1: 1 mark newformat2: 8 mark newformat3: And the % 1 want to know whose % 2 you % 3arguments. length: 4 Mark newformat0: % 2 Mark newformat1: 2 Mark newformat2: 30 mark newformat3: And the % 1 want to know whose % 2 you % 3arguments. length: 4 newformat0: % 3 newformat1: 3 newformat2: 37 newformat3: And the % 1 want to know whose % 2 you % 3arguments. length: 3 newmyreplace0: cj90 newmyreplace1: 0 newmyreplace2: cj9080, cj8976, cj12919, cj8765arguments. length: 3 newmyreplace0: cj89 newmyreplace1: 7 newmyreplace2: cj9080, cj8976, cj12919, cj8765arguments. length: 3 newmyreplace0: cj12 newmyreplace1: 14 newmyreplace2: cj9080, cj8976, cj12919, cj8765arguments. length: 3 newmyreplace0: cj87 newmyreplace1: 22 newmyreplace2: cj9080, cj8976, cj12919, cj8765

ForArgumentsThe value is clear now,ArgumentsThe number difference should be related to the regular expression we write. In any case, the first parameter is the matched string and the last one is the original string, the second-to-last parameter is the start position of the matched string in the original string index, suchFormatThe second parameter inIndexDepends on the situation. I wrote it myself.NewmyreplaceThis parameter is not found in,FormatOfIndexThe parameter is% [1-4],Inside1-4But write a method to determine:

 

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 thatStringOfReplaceIt is quite powerful, but I am not strong enough in regular expressions. I don't know what other special regular expressions will produce different results. I don't know who hasJavascriptInsideStringClassReplaceI hope to contribute to the original writing. I 'd like to study it well.

 

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.