Use of the Replace method for JavaScript

Source: Internet
Author: User

Replace is a method on a string object that enables you to replace some of the specified substrings in a string with another string. The usage is "string. Replace (Parm1,parm2)". Where Parm1 's old string can make a regular string, or it can be a regular expression; Parm2 the result of a return, can be a string, and more importantly, it can also be a JavaScript method, as a callback function. Here are some small examples to illustrate.

The code is as follows Copy Code

Alert (' Abcabd '. replace (' AB ', ' 12 '));

The result of alert here is 12CABD, noting that it only replaces the first occurrence and does not replace it. If the string is all replaced, it can only be done using the regular expression method.

The code is as follows Copy Code

Alert (' Abcabdabe '. Replace (/ab/g, ' 12 '));

The resulting result is a global replacement for 12c12dabe,g representations, and I can be used to ignore case, noting that regular expressions cannot be enclosed in quotes.

The code is as follows Copy Code

var i = 0;
Alert (' Abaabbabc '. Replace (/ab/g, function (m) {
i++;
return m + '-' + i + '-';
}));

The result here is ab-1-aab-2-bab-3-c, which, when matched to a substring, invokes the callback method and passes the matching value as a parameter. In another example, replace the Chengshing number with a number less than 30 in the string

The code is as follows Copy Code

Alert ('/d+/g '. Replace, function (match) {
return parseint (Match) < 30? ' * ': match;
}));


The following shows the repalce of several JavaScript regular expressions, some of which we rarely see elsewhere, such as the second and third-party methods.

The code is as follows Copy Code

The following example is used to get two parameters of the URL and return the real URL before urlrewrite
var reg=new RegExp ("() (\d+), (\d+). aspx", "GMI");
var url= "1017141,20361055.aspx";

Way one, the simplest common way
var rep=url.replace (Reg, "$1showbook.aspx?bookid=$2&chapterid=$3");
Alert (rep);

Method two, using the callback function of the fixed parameter
var rep2=url.replace (reg,function (M,P1,P2,P3) {return p1+ "showbook.aspx?bookid=" +p3+ "&chapterid=" +p3});
alert (REP2);

Method III, using a callback function with a non fixed parameter
var rep3=url.replace (Reg,function () {var args=arguments; return args[1]+ showbook.aspx?bookid= "+args[2]+" & Chapterid= "+args[3];}";
alert (REP3);


Method Four
Mode four and method three are very similar, in addition to returning the replacement string, you can also get the parameters alone
var BookID;
var Chapterid;
function Captext ()
{
var args=arguments;
BOOKID=ARGS[2];
CHAPTERID=ARGS[3];
return args[1]+ "showbook.aspx?bookid=" +args[2]+ "&chapterid=" +args[3];
}

var rep4=url.replace (Reg,captext);
alert (REP4);
alert (BookID);
alert (Chapterid);


In addition to using the Replace method to get the group of regular representations, you can also use test, the Exec method to get the grouping, but the technique is different.
var reg2=new RegExp ("() (\d+), (\d+). aspx", "GMI");
var m=reg2.exec ("1017141,20361055.aspx");
var s= "";
Get all the groupings
for (i = 0; i < m.length; i++) {
s = s + m[i] + "n";
}
alert (s);

BOOKID=M[2];
CHAPTERID=M[3];
alert (BookID);
alert (Chapterid);


To get a group by using the test method
var reg3=new RegExp ("() (\d+), (\d+). aspx", "GMI");
Reg3.test ("1017141,20361055.aspx");
Get three groups
alert (regexp.$1);
alert (regexp.$2);
alert (regexp.$3);

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.