Js Regular Expression/replace variable replacement method

Source: Internet
Author: User

Recently, project tasks are heavy, and blog updates are slow, but you may wish to share your accumulated resources.
JavaScript Regular Expressions (updated based on recent writes)
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 real Url before urlRewrite.
Var reg = new RegExp ("(http://www.qidian.com/BookReader/) (\ d +), (\ d +). aspx", "gmi ");
Var url = "http://www.qidian.com/BookReader/1017141,20361055.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 );
// Use the test method to obtain the group
Var reg3 = new RegExp ("(http://www.qidian.com/BookReader/) (\ d +), (\ d +). aspx", "gmi ");
Reg3.test ("http://www.qidian.com/BookReader/1017141,20361055.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].
Author: pingpang

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.