How to Use the replace method in javascript

Source: Internet
Author: User

The replace method is a commonly used character replacement method in Javascript. It can directly replace a specified string or write multiple regular expression rules in the replace method to replace, the following describes how to use the replace method.

Replace is a method on the String object. It can replace some specified substrings in the String with other strings. The usage is "string. replace (parm1, parm2 )". The old string of parm1 can be a normal string or a regular expression. The return result of parm2 can be a string, and more importantly, it can be a javascript method, as the callback function. Below are several small examples to illustrate.

The Code is as follows: Copy code

Alert ('abcabd'. replace ('AB', '12 '));

Here the alert result is 12 cabd. Note that it is only replaced with the one that appears for the first time and will not be replaced later. If you replace all substrings, you can only use the regular expression method.

The Code is as follows: Copy code

Alert ('abcabdab'. replace (/AB/g, '12 '));

The result is 12c12dAbe. g indicates global replacement. You can also use I to ignore case sensitivity. Note that regular expressions cannot be enclosed by quotation marks.

The Code is as follows: Copy code

Var I = 0;
Alert ('ababbabc'. replace (/AB/g, function (m ){
I ++;
Return m + '-' + I + '-';
}));

The result here is a ab-1-Aab-2-Bab-3-C. When a substring is matched, the callback method is called and the matched value is passed in as a parameter. In another example, replace the number smaller than 30 in the string with the star number.

The Code is as follows: Copy code

Alert ('10 33 21 18 52 '. replace (/d +/g, function (match ){
Return parseInt (match) <30? '*': Match;
}));


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 Code is as follows: Copy code

// The following example is used to obtain two url parameters and return the real Url before urlRewrite.
Var reg = new RegExp ("() (\ d +), (\ d +). aspx", "gmi ");
Var url = "1017141,203 61055. 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.
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.
Var reg2 = new RegExp ("() (\ d +), (\ d +). aspx", "gmi ");
Var m1_reg2.exe c ("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 );


// Use the test method to obtain the group
Var reg3 = new RegExp ("() (\ d +), (\ d +). aspx", "gmi ");
Reg3.test ("1017141,203 61055. aspx ");
// Obtain three groups
Alert (RegExp. $1 );
Alert (RegExp. $2 );
Alert (RegExp. $3 );

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.