Introduction to regular expression instances

Source: Internet
Author: User

I haven't read the regular expression for a long time. I happen to use it today. I just learned how to read a book.
In 50%, the original practices are described in the opposite way.
I. Basic knowledge about Javascript Regular Expressions
1. Create and use a JavaScript Regular object
Declare a JavaScript Regular Expression CopyCode The Code is as follows: var recat = new Regexp ("cat ");
You can also
VaR recat =/CAT/; // Perl style (recommended)

2. Learn the most common test exec match search replace split six methods.
1) test checks whether the specified string existsCopy codeThe Code is as follows: 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 valueCopy codeThe Code is as follows: 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 ArrayCopy codeThe Code is as follows: 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
}

4) The returned search location is similar to indexof.Copy codeThe Code is as follows: var DATA = "123123,213,123, 3, Cat, Cat, dsfsdfs ,";
VaR recat =/CAT/GI;
Alert (data. Search (recat); // 23

5) replace replacement characters with regular expressionsCopy codeThe Code is as follows: 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.Copy codeThe Code is as follows: 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. Learning simple class negative class range class combination class Copy code The Code is as follows: // simple class
VaR DATA = "1 libinqq, 2 libinqq, 3 libinqq, 4 libinqq ";
VaR recat =/[123] libinqq/GI;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]); // 1 libinqq 2 libinqq 3 libinqq
}
// Negative
VaR DATA = "alibinqq, 1 libinqq, 2 libinqq, 3 libinqq, 4 libinqq"; // \ u0062cf
VaR recat =/[^ A123] libinqq/GI;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]); // 4 libinqq
}
// Range
VaR DATA = "libinqq1, libinqq2, libinqq3, libinqq4, libinqq5"; // \ u0062cf
VaR recat =/libinqq [2-3]/GI;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]); // libinqq2 libinqq3
}
// Combination
VaR DATA = "a, B, c, W, 1, 2, 3, 5"; // \ u0062cf
VaR recat =/[a-q1-4 \ n]/GI;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]); // a B C 1 2 3
}

these are the most basic methods for using JS regular expressions. If you cannot see them, copy them to your notebook and read them.
two Javascript Regular Expressions are grouping knowledge
1) Simple grouping copy Code the code is as follows:

2 complex grouping copy Code the code is as follows:

3 reverse reference copy Code the code is as follows:

4 candidate copy Code the code is as follows:

5 Non-capturing group copy Code the code is as follows:

6. foresightCopy codeThe Code is as follows: <script language = "JavaScript">
<! --
/* Regular Expression foresight
The forward direction is the same as its name. It tells the regular expression generator to look forward to some characters rather than moving positions.
*/
VaR stomatroom = "bedroom ";
VaR stomatch2 = "bedding ";
VaR rebed =/bed (? = Room )/;
Alert (rebed. Test (stomatch1); // true
Alert (rebed. Test (stomatch2); // false
// Negative foresight
VaR stomatroom = "bedroom ";
VaR stomatch2 = "bedding ";
VaR rebed =/bed (?! Room )/;
Alert (rebed. Test (stomatch1); // false
Alert (rebed. Test (stomatch2); // true
// -->
</SCRIPT>

7 Border Copy code The Code is as follows: <script language = "JavaScript">
<! --
/* Regular Expression Boundary
^ Start with line
$ End of row
\ B word boundary
\ B Non-word boundary
*/
VaR stomatch = "important word is the last one .";
VaR relastword =/(\ W +) \. $ /;
Relastword. Test (stomatch );
Alert (Regexp. $1); // One
/*
If you want to find a word, but want it to appear only at the end of the row, you can use the dollar sign ($) to represent it:
*/
VaR stomatch = "important word is the last one .";
VaR relastword =/^ (\ W + )/;
Relastword. Test (stomatch );
Alert (Regexp. $1); // important
/*
In this example, the regular expression looks for one or more word characters after the start position of a row. If you encounter non-word characters
If the match is stopped, important is returned. This example can also be implemented using word boundaries.
*/
VaR stomatch = "important word is the last one .";
VaR relastword =/^ (. + ?) \ B /;
Relastword. Test (stomatch );
Alert (Regexp. $1); // important
/*
Here, regular expressions use inert quantifiers to indicate that any character can appear before the word boundary and can appear once or
Multiple times (if greedy quantifiers are used, the expression matches the entire string ).
*/
VaR DATA = "first second thind fourth th Sixth ";
VaR recat =/\ B (\ s + ?) \ B/g;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}
/*
Word boundary can be used to easily extract words from strings.
*/
// -->
</SCRIPT>

8 multi-row Mode
CodeCopy codeThe Code is as follows: <script language = "JavaScript">
<! --
/* Regular Expression multi-row Mode
To create a multi-row mode, you only need to specify a word at the end of the row to be matched by the regular expression.
*/
VaR DATA = "first second \ n thind fourth \ n th Sixth ";
VaR recat =/(\ W +) $/g;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}
/*
Only one word Sixth is returned. Because the linefeed blocks matching, only one word at the end of the row can be matched,
Of course, you can also use the split () method to split the string into an array, but you have to match each row separately.
In the past, I used to read books and read them in half. As a result, I used a lot of splits, which is actually very simple as shown below.
In this example, only the M parameter is required for multi-row matching.
*/
VaR DATA = "first second \ n thind fourth \ n th Sixth ";
VaR recat =/(\ W +) $/GM;
VaR arrdata = data. Match (recat );
For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}
// -->
</SCRIPT>

at this point, these are the basic methods of JavaScript Regular Expressions. If you see complicated regular expressions, you will suddenly feel open.

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.