How to replace a variable with the js Regular Expression replace

Source: Internet
Author: User
This article mainly introduces the method of replacing variables with the regular expression replace in js. Recently, the project has been heavy and the blog will be updated slowly, but I hope to share my accumulation with you, for more information about JavaScript regular expressions, see the latest Regular Expressions)

1. Creation and usage of javascript Regular object replacement:/pattern/flags first, a simple case study to understand what replace can do

Regular Expression constructor: new RegExp ("pattern" [, "flags"]);
Regular Expression substitution variable function: stringObj. replace (RegExp, replace Text );

Parameter description:

Pattern -- a regular expression text
Flags -- if it exists, it will be the following values:
G: Global match
I: case insensitive
Gi: combination of the above

// The following example is used to obtain two url parameters and return the true Urlvar reg = new RegExp ("( http://www.qidian.com/BookReader/ ) (\ D +), (\ d +). aspx "," gmi "); var url =" http://www.qidian.com/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); // obtain the group var reg3 = new RegExp ("( http://www.qidian.com/BookReader/ ) (\ D +), (\ d +). aspx "," gmi "); reg3.test (" http://www.qidian.com/BookReader/ 1017141,203 61055. aspx "); // obtain three groups: alert (RegExp. $1); alert (RegExp. $2); alert (RegExp. $3 );

2. Learn the six most common test exec match search replace split methods.

1) test checks whether the specified string exists
Var data = 123123 ″;
Var reCat =/123/gi;
Alert (reCat. test (data); // true
// Check whether the character g exists. continue to the next step. I is case insensitive.

2) exec returns the query value
Var data = "123123,213,123, 3, Cat, cat, dsfsdfs ,";
Var reCat =/cat/I;
Alert(reCat.exe c (data); // Cat

3) match to get the query Array
Var data = "123123,213,123, 3, Cat, cat, dsfsdfs ,";
Var reCat =/cat/gi;
Var arrMactches = data. match (reCat)
For (var I = 0; I <arrMactches. length; I ++)
{
Alert (arrMactches [I]); // Cat cat
}

4) The returned search location is similar to indexof.
Var data = "123123,213,123, 3, Cat, cat, dsfsdfs ,";
Var reCat =/cat/gi;
Alert (data. search (reCat); // 23

5) replace replacement characters with regular expressions
Var data = "123123,213,123, 3, Cat, cat, dsfsdfs ,";
Var reCat =/cat/gi;
Alert (data. replace (reCat, "libinqq "));

6) split splits the array using regular expressions.
Var data = "123123,213,123, 3, Cat, cat, dsfsdfs ,";
Var reCat = /\,/;
Var arrdata = data. split (reCat );
For (var I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}
3. Common expression collection:

"^ \ D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? \ D + $ "// integer
"^ \ D + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0)
"^ ([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"
// Positive floating point number
"^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)
"^ (-([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"
// Negative floating point number
"^ (-? \ D +) (\. \ d + )? $ "// Floating point number
"^ [A-Za-z] + $" // A string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ \ W + $" // a string consisting of a number, 26 English letters, or underscores
"^ [\ W-] + (\\. [\ w-] +) * @ [\ w-] + (\\. [\ w-] +) + $ "// email address
"^ [A-zA-z] +: // (\ w + (-\ w + )*)(\\. (\ w + (-\ w + )*))*(\\? \ S *)? $ "// Url
"^ [A-Za-z0-9 _] * $ ".

Basic knowledge of Regular Expressions

^ Matches the beginning of an input or a line,/^ A/matches "An a", but does not match "an"
$ Matches the end of An input or line,/a $/matches "an a", but does not match "An"
* Match the first metacharacters 0 or multiple times./ba */match B, ba, baa, baaa
+ Match the first metacharacters once or multiple times./ba +/match the first metacharacters with ba, baa, and baaa
? Match the first metacharacters 0 or 1 time,/ba? /Match B, ba
(X) Match x and save x in the variable $1... $9.
X | y matches x or y
{N} exact match n times
{N,} match more than n times
{N, m} matches n-m times
[Xyz] character set (character set) that matches any one of the set's characters (or metacharacters)
[^ Xyz] does not match any character in this set
[\ B] matches a return character.
\ B matches the boundary of a word
\ B matches the non-boundary of a word
\ CX here, X is a control character, // \ cM/matches Ctrl-M
\ D matches one word character,/\ d/=/[0-9]/
\ D matches a non-word character,/\ D/=/[^ 0-9]/
\ N matches a linefeed.
\ R matches a carriage return.
\ S matches a blank character, including \ n, \ r, \ f, \ t, \ v, etc.
\ S matches a non-blank character, equal to/[^ \ n \ f \ r \ t \ v]/
\ T matches a tab
\ V matches a straight-heavy Tab
\ W matches a character that can comprise a word (alphanumeric, this is my free translation, including numbers), including underscores, such as [\ w] matching 5 in "$5.98, equal to [a-zA-Z0-9]
\ W matches a character that cannot comprise a word, such as [\ W] matching $ in "$5.98", equal to [^ a-zA-Z0-9].


For more articles about how to replace variables with js regular expressions, refer to PHP!

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.