The replacement parameter of the replace () method can be a function rather than a string. In this case, each match calls this function, and the string it returns will be used as the replacement parameter for replacing the text using the replace () method. It can be a function rather than a string. In this case, each match calls this function, and the string it returns will be used as the replacement text. The first parameter of this function is a matching string. The following parameter is a string that matches the subexpression in the pattern. There can be 0 or more such parameters. The following parameter is an integer that declares the position where the matching occurs in the stringObject. The last parameter is stringObject itself.
The following describes several repalce methods with javascript Regular Expressions. Some methods are rarely seen elsewhere, such as the second and third-party methods.
// The following example is used to obtain two url parameters and return the real Url before urlRewrite.
The Code is as follows:
Var reg = new RegExp ("(http://www.qidian.com/BookReader/) (\ d +), (\ d +). aspx", "gmi ");
Var url = "http://www.qidian.com/BookReader/1017141,20361055.aspx ";
// Method 1: the simplest and most common method
Var rep = url. replace (reg, "$ 1ShowBook. aspx? BookId = $2 & chapterId = $3 ");
Alert (rep );
// Method 2: Use a callback function with fixed parameters
Var rep2 = url. replace (reg, function (m, p1, p2, p3) {return p1 + "ShowBook. aspx? BookId = "+ p3 +" & chapterId = "+ p3 });
Alert (rep2 );
// Method 3: callback function with non-fixed parameters
Var rep3 = url. replace (reg, function () {var args = arguments; return args [1] + "ShowBook. aspx? BookId = "+ args [2] +" & chapterId = "+ args [3];});
Alert (rep3 );
// Method 4
// Method 4 is similar to method 3. Besides returning the replaced string, you can also obtain parameters separately.
The Code is as follows:
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 obtain the regular expression grouping, you can also use the test and exec methods to obtain the grouping, but the method is different.
The Code is as follows:
Var reg2 = new RegExp ("(http://www.qidian.com/BookReader/) (\ d +), (\ d +). aspx", "gmi ");
Var m=reg2.exe c ("http://www.qidian.com/BookReader/1017141,20361055.aspx ");
Var s = "";
// Obtain all Groups
The Code is as follows:
For (I = 0; I <m. length; I ++ ){
S = s + m [I] + "\ n ";
}
Alert (s );
BookId = m [2];
ChapterId = m [3];
Alert (bookId );
Alert (chapterId );
// Use the test method to obtain the group
The Code is as follows:
Var reg3 = new RegExp ("(http://www.qidian.com/BookReader/) (\ d +), (\ d +). aspx", "gmi ");
Reg3.test ("http://www.qidian.com/BookReader/1017141,20361055.aspx ");
// Obtain three groups
The Code is as follows:
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 {
Document. write (arguments [I] +"
");
}
Document. write ("-------------------------------------------------
");
});
Two examples (proving that the results of the replace input regular parameters and the character passing parameters are different ):
Alert ("123". replace ("1", function () {var un; return un;}); // The undefined23 is displayed.
Alert ("123". replace (new RegExp ("1"), function () {var un; return un;}); // pop up 23