The first parameter of the function is a string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.
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.
Copy Code code as follows:
The following example is used to get two parameters of the URL and return the real URL before urlrewrite
var reg=new RegExp ("(http://www.jb51.net/BookReader/) (\\d+), (\\d+). aspx", "GMI");
var url= "http://www.jb51.net/BookReader/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 ("(http://www.jb51.net/BookReader/) (\\d+), (\\d+). aspx", "GMI");
var m=reg2.exec ("http://www.jb51.net/BookReader/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 ("(http://www.jb51.net/BookReader/) (\\d+), (\\d+). aspx", "GMI");
Reg3.test ("http://www.jb51.net/BookReader/1017141,20361055.aspx");
Get three groups
alert (regexp.$1);
alert (regexp.$2);
alert (regexp.$3);
var str= "www.baidu.com";
Str.format ("Good", "Q")
Str.replace (new RegExp (\.) (Bai) du "," g "), function () {
for (Var i=0;i<arguments.length;i++)
{
document.write (arguments[i]+ "<br/>");
}
document.write ("-------------------------------------------------<br/>");
});
Two examples (proof, replace incoming regular parameter and character Fu parameter result is different):
Alert ("123". Replace ("1", function () {var un;return un;}); Pop-up Undefined23
Alert ("123". Replace (new RegExp ("1"), function () {var un;return un;}); Pop up 23