Javascript notes -- some things about the String replace Function

Source: Internet
Author: User

The basic knowledge of javascript reinforcement is designed to pave the way for future research on jQuery source code.

I recently consulted javascript and 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

Such functions may have been seen in shell or java, but the methods implemented in javascript Functions are novel. The novelty is:

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:

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:

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:

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);//4return args[index];});}function myReplaceFtn(s){return String(s).replace(/CJ[0-9]{2}/g,function(word,index){alert("arguments.length:" + arguments.length);//3return 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:

// The following program runs 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

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:

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 'thisisB';case 'c':    return 'thisisC';case 'd':    return 'thisisD';default:    return 'thisisNULL';}});}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.

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.