Regular expression syntax of replace in Javascript

Source: Internet
Author: User
The following is an example of the replace method in Javascript. The following example demonstrates how the replace method replaces the first word "the" with the word ". Function replacedemo () {var R, RE; // declare the variable. VaR Ss = "the man hit the ball with the bat. \ n "; SS + =" while the fielder caught the ball with the glove. "; Re =/The/g; // create the regular expression mode. R = ss. Replace (Re, "A"); // Replace "the" with "". Return (r); // return the replaced string .} In addition, the replace method can replace the subexpression in the mode. The following example demonstrates each pair of words in the exchange string: function replacedemo () {var R, RE; // declare the variable. VaR Ss = "The rain in Spain falls mainly in the plain. "; Re =/(\ s +)/g; // create the regular expression mode. R = ss. Replace (Re, "$3 $2 $1"); // exchange each pair of words. Return (r); // return the result string .} The following example (executed in JScript 5.5 and later versions) is a conversion from Fahrenheit to Celsius, which demonstrates the use of functions as replacetext. To know how the function works, pass a string containing a value followed by "F" (for example, "water boils at 212 "). Function f2c (s) {var test =/(\ D + (\. \ D *)?) F \ B/g; // initialization mode. Return (S. replace (test, function ($0, $1, $2) {return ($1-32) * 5/9) + "C ");}));} document. write (f2c ("Water freezes at 32f and boils at 212f. "); the focus is the third example 1. search on some websites Article When the search keyword is highlighted and the color is changed ?? How is this implemented ?? In fact, we can use a regular expression to implement it. How can we implement it? For the simple principle, see the following Code <Script language = "JavaScript"> var STR = "People's Republic of China, People's Republic of China"; var newstr = Str. replace (/(person)/g, "<font color = Red> $1 </font>"); document. write (newstr); </SCRIPT> 2. <script language = "JavaScript"> var Reg = new Regexp ("\ D", "G"); var STR = "abd1afa4sdf"; Str. replace (Reg, function () {alert (arguments. length) ;}); </SCRIPT> we are surprised to find that the anonymous function is actually executed twice, and there are three parameters in the function. Why is it executed twice ?? It is easy to think that because the regular expression we write matches a single number, and the detected string has exactly two digits, the anonymous function is executed twice .. What are the three parameters in an anonymous function? In the end, we will find that the first parameter represents the matched characters, and the second to last parameter represents the minimum index position (Regexp. index), the first to last parameter indicates the matched string (Regexp. input ). In fact, the number of these parameters will increase as the sub-match increases. That is to say, we can understand that $0, $1, and $2 in the JavaScript document are just form parameters. The real parameters are written by regular expressions, even if we write them as A, B, C, it still has the value $0, $1, $2.

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.