JS String class Replace function

Source: Internet
Author: User
Tags regular expression


Any of the following matching variables can be used to identify the latest match and to find a matching string. You can use a matching variable in a text replacement that requires a dynamic decision to replace a string.

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

The functions of this function, in the shell or Java have been seen, but in the Web page effects function to implement the method is very novel. The new place is in:

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:


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:


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:


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:


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 ';
});
}


Result:
arguments.length:4
marked newformat0:%1
Mark newformat1:1
logo newformat2:8
Mark Newformat3:and the %1 want to know whose%2
Arguments.length:4
Mark newformat0:%2
Mark Newformat1:2
Sign newformat2:30
Mark NE Wformat3:and the%1 want to know whose%2
Arguments.length:4
labeled newformat0:%3
Mark Newformat1:3
Newfo Rmat2:37
Mark Newformat3:and the%1 want to know whose%2 you%3
Arguments.length:3
Mark newmyreplace0:cj90
Sign NE wmyreplace1:0
Mark newmyreplace2:cj9080,cj8976,cj12919,cj8765
Arguments.length:3
Mark newmyreplace0:cj89
Mark Newmyreplace1:7
to 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
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:

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. Besides, I don't know who has JavaScript. String Class Replace Original

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.