Let's talk about the replace () function in JavaScript.

Source: Internet
Author: User

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 example below is used to get the two parameters of the url and return the true Urlvar reg = new RegExp ("(http://www.bkjia.com/BookReader/) (\ d +) before urlRewrite ), (\ d + ). aspx "," gmi "); var url =" http://www.bkjia.com/BookReader/1017141,20361055.aspx "; // method 1, the simplest and most commonly used method 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, exec method to obtain the group, but the method is different var reg2 = new RegExp ("(http://www.bkjia.com/BookReader/) (\ d +), (\ d + ). aspx "," gmi "); var m = reg2.exec (" http://www.bkjia.com/BookReader/1017141,20361055.aspx "); var s =" "; // get 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.bkjia.com/BookReader/) using the test method) (\ d +), (\ d + ). aspx "," gmi "); reg3.test (" http://www.bkjia.com/BookReader/1017141,20361055.aspx "); // gets three groups of alert (RegExp. $1); alert (RegExp. $2); alert (RegExp. $3); var str = "www.bkjia.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 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

Related Article

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.