JavaScript notes String class Replace function some things _javascript tips

Source: Internet
Author: User
I recently checked the JavaScript data and found a function:
Copy Code code 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"%1 want to know whose%2 "," Papers "," shirt "," wear "));
And the papers want to know whose shirt you wear

Functions like this are seen in the shell or Java, but the method of implementing JavaScript functions is novel. The new place is in:
Copy Code code as follows:

return String (s). Replace (Pattern,function (word,index) {
return Args[index];
});

But here the use of the string class replace is quite different from what I used to do, and I've written a function like replace before:
Copy Code code 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, basically not thinking about its second, third or more parameters, now see someone using the second argument, Would like to explore the next replace the second parameter to use the function time, the inside parameters in the end how many, each meaning in the end?
Here's how I rewrote the replacement function I wrote myself:
Copy Code code 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, in the function format, functions (Word,index), which I think should be the index of the matching string of the regular expression (% 1 index is 1,%2 index is 2,%3 index is 3), And the second parameter in the function I wrote is not matched to the index of the string, but rather the position of the character being matched to the original string. I've done the following tests:
Copy Code code 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 + ' @ ';
});
}

There are 4 parameters in the function format (word,index), and there are 3 parameters for function myreplaceftn (s) inside functions (Word,index). Why is there such a difference? I did the following tests:
Copy Code code as follows:

The following programs run 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 ("Marked 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 ';
});
}

Results:
Arguments.length:4
Mark newformat0:%1
Mark Newformat1:1
Mark Newformat2:8
Mark Newformat3:and the%1 want to know whose%2/%3
Arguments.length:4
Mark Newformat0:%2
Mark Newformat1:2
Mark Newformat2:30
Mark Newformat3:and the%1 want to know whose%2/%3
Arguments.length:4
Mark Newformat0:%3
Mark Newformat1:3
Mark Newformat2:37
Mark Newformat3:and the%1 want to know whose%2/%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
The arguments value in the callback function is now clearer, and the number of arguments should be related to the regular expression we write, anyway, the first argument is the string to match, the last one is the original string, The penultimate argument is the starting bit of the string that matches to the index of the original string, as in the second parameter in format the index is determined according to the situation, I wrote the newmyreplace without this parameter, the index parameter of the format is%[1-4], inside the 1-4, However, write a method to determine the following:
Copy Code code 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 ' 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 for you%b", "Papers", "shirt", "wear"));
And the Thisisa want to know whose THISISD you thisisb

This shows that the replace of string is quite powerful, but my regular expression is not enough, I do not know what other special regular expressions will produce different results. Also do not know who has JavaScript inside the string class replace the original writing, I hope to contribute, I would 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.