I recently checked the javascript information 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
Functions with this kind of function have been seen before in shell or java, but the method of implementing it in javascript function is very novel. The novelty is:
return String(s). replace(pattern,function(word,index){
return args[index];
});
But the usage of replace in the String class here is very different from what I usually use. Similarly, I have written a replace function like this before:
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 and basically don’t think about the second, third or more parameters. Now I see someone using it. As for the second parameter, I really want to explore how many parameters are there and what is the meaning of each one when the second parameter of replace is used in function?
The following 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
Originally, I thought that the function(word,index) in the function format should be the regular expression. The index of the matching string (the index of %1 is 1, the index of %2 is 2, and the index of %3 is 3), and the second parameter index in the function I wrote is not the index of the string that is matched, but The position of the matched character in the original string. Below I did a test like this:
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 "@";
});
}
function inside function format There are 4 parameters for (word, index), and there are 3 parameters for function (word, index) in function myReplaceFtn(s). Why is there such a difference? I did the following test:
//The following program works in firefox Run inside
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{
console.log("mark 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{
console.log ("mark 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 %3
arguments.length:4
Mark newformat0: %2
Mark newformat1: 2
Mark newformat2: 30
Mark newformat3: And the %1 want to know whose %2 you %3
arguments.length:4
mark newformat0: %3
mark newformat1: 3
mark newformat2: 37
mark newformat3: And the %1 want to know whose %2 you %3
arguments.length:3
Mark newmyReplace0: CJ90
Mark newmyReplace1: 0
Mark newmyReplace2: CJ9080,CJ8976,CJ12919,CJ8765
arguments.length:3
Mark newmyReplace0 : CJ89
Mark newmyReplace1: 7
Mark newmyReplace2: CJ9080, CJ8976, CJ12919, CJ8765
arguments.length: 3
Mark newmyReplace0: CJ12
Mark newmyReplace1: 14
Mark newmyReplace2 : CJ9080,CJ8976,CJ12919,CJ8765
arguments.length:3
Mark newmyReplace0: CJ87
Mark newmyReplace1: 22
Mark newmyReplace2: CJ9080,CJ8976,CJ12919,CJ8765
For the callback function The arguments value is now clearer. The difference in the number of arguments should be related to the regular expression we wrote. In any case, the first parameter is the matched string, the last one is the original string, and the penultimate parameter is The matched string is at the starting position of the original string index. Like the second parameter index in format, it depends on the situation. There is no such parameter in newmyReplace that I wrote myself. The index parameter of format is %[1-4 ], 1-4 in it, but still write a method to confirm:
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 replacement of String is quite powerful, but my regular expression skills are not enough, I don’t know what else What other special regular expressions will produce different results? In addition, I don’t know if anyone has the original writing method of String class replace in JavaScript. I hope they can contribute it. I want to study it carefully.