The replacement parameter of the replace () method can be a function rather than a string. In this
I have previously written an article about the use of the Replace () function and used it to highlight search keywords. However, this function is not detailed here.
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 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 shows several repalce methods of javascript Regular Expression. 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 true Urlvar reg = new RegExp ("( http://www.nowamagic.net/BookReader/ ) (\ D +), (\ d +). aspx "," gmi "); var url =" http://www.nowamagic.net/BookReader/ 1017141,203 61055. aspx "; // method 1. The most common method is var rep = url. replace (reg," $ 1ShowBook. aspx? BookId = $2 & chapterId = $3 "); alert (rep); // method 2. Use the callback function var rep2 = url with fixed parameters. replace (reg, function (m, p1, p2, p3) {return p1 + "ShowBook. aspx? BookId = "+ p3 +" & chapterId = "+ p3}); alert (rep2); // method 3: callback function var rep3 = url with unfixed parameters. 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 obtain the var bookId, var chapterId, and function capText () parameters separately () {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); // except for the group that uses the replace method to obtain the regular expression, you can also use the test and exec methods to obtain the group, but the method is different. var reg2 = new RegExp ("( http://www.nowamagic.net/BookReader/ ) (\ D +), (\ d +). aspx "," gmi "); var m = reg2.exec (" http://www.nowamagic.net/BookReader/ 1017141,203 61055. aspx "); var s =" "; // obtain all groups for (I = 0; I <m. length; I ++) {s = s + m [I] + "\ n";} alert (s); bookId = m [2]; chapterId = m [3]; alert (bookId); alert (chapterId); // obtain the group var reg3 = new RegExp ("( http://www.nowamagic.net/BookReader/ ) (\ D +), (\ d +). aspx "," gmi "); reg3.test (" http://www.nowamagic.net/BookReader/ 1017141,203 61055. aspx "); // obtain the three groups alert (RegExp. $1); alert (RegExp. $2); alert (RegExp. $3); var str = "www.nowamagic.com"; // str. format ("good", "q") str. replace (new RegExp ("(\\.) (bai) du "," g "), function () {for (var I = 0; I");} document. write ("-------------------------------------------------
");});
Two examples show 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 undefined23alert ("123 ". replace (new RegExp ("1"), function () {var un; return un;}); // pop up 23
This article is available at http://www.nowamagic.net/librarys/veda/detail/459.