Javascript Regular Expression

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

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 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, 312, 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, 312, 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.

VaR DATA = "123123,213,123, 312, 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, 312, 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, 312, 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

// 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.

Second, JavaScript Regular Expressions are grouping knowledge.

1) Simple grouping

Code
<Script language = "JavaScript">
<! --
/* Simple grouping of Regular Expressions
For example, we want to find the string mousemouse.

VaR recat =/mousemouse/GI;

Although this is acceptable, it is a waste. What should I do if I don't know how many times the mouse appears in the string.
VaR recat =/(Mouse) {2}/GI; the brackets indicate that the column mouse appears twice in a row.

*/

VaR DATA = "ah-mousemouse ";
VaR recat =/(Mouse) {2}/GI;
VaR arrdata = data. Match (recat );

For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}

// -->
</SCRIPT>

2. Complex grouping

Code
<Script language = "JavaScript">
<! --
/* Groups with complex Regular Expressions
? Zero or once
* Zero or multiple times
+ At least once or multiple times
*/

VaR DATA = "BB ba da bad dad AA ";
VaR recat =/([Bd] ad ?) /GI; // match the ba da bad dad
VaR arrdata = data. Match (recat );

For (VAR I = 0; I <arrdata. length; I ++)
{
Alert (arrdata [I]);
}

// At the same time, do not mind placing the group in the middle of the Group
// Var Re =/(MOM (and Dad )?) /; Match Mom or Mon and daa
// -->
</SCRIPT>

3. Reverse reference

Code
<Script language = "JavaScript">
<! --
/* Regular Expression reverse reference */

VaR stomatch = "#123456789 ";
VaR renumbers =/# (\ D + )/;
Renumbers. Test (stomatch );
Alert (Regexp. $1 );

/*
In this example, we try to match the pound following several or more numbers and group the numbers.
To store them. After the test method is called, all reverse references are saved to the Regexp constructor.
Starting from Regexp. $1 (which saves the first reverse reference), if there is a second reverse reference
Regexp. $2. If there is a third reverse reference, it is Regexp. $3. And so on. Because this group
Match "123456780", so this string is stored in Regexp. $1.

*/

VaR stochange = "1234 5678 ";
VaR rematch =/(\ D {4}) (\ D {4 })/;
VaR snew = stochange. Replace (rematch, "$2 $1 ");
Alert (snew );

/*
In this example, a regular expression has two groups, each of which has four numbers. The second parameter in the Replace () method
In, $2 is equivalent to "5678", while $1 is equivalent to "1234", which corresponds to the order in which they appear in the expression.
*/

// -->
</SCRIPT>

4 candidates

Code
<Script language = "JavaScript">
<! --
/* Regular Expression candidate */
VaR stomatch1 = "red ";
VaR stomatch2 = "black ";
VaR rered =/red /;
VaR reblack =/Black /;

Alert (rered. Test (stomatch1) | reblack. Test (stomatch1 ));
Alert (rered. Test (stomatch2) | reblack. Test (stomatch2 ));

/*
Although this can complete the task, it is very long, and there is another way of doing this is the candidate operator of the regular expression.
*/

VaR stomatch1 = "red ";
VaR stomatch2 = "black ";
VaR reredorblack =/(Red | black )/;
Alert (reredorblack. Test (stomatch1 ));
Alert (reredorblack. Test (stomatch2 ));

// -->
</SCRIPT>

5 Non-capturing Group

Code
<Script language = "JavaScript">
<! --
/* Non-capturing grouping of Regular Expressions

If you want to create a non-capturing group, add a question mark and a colon following the left parenthesis:
*/

VaR stomatch = "#123456789 ";
VaR renumbers = /#(? : \ D + )/;
Renumbers. Test (stomatch );
Alert (Regexp. $1 );

/*
The last line in this exampleCodeOutput an empty string because the group is non-capturing,
*/

VaR stomatch = "#123456789 ";
VaR renumbers = /#(? : \ D + )/;
Alert (stomatch. Replace (renumbers, "ABCD $1 "));

/*
Because of this, the Replace () method cannot use any reverse reference using the Regexp. $ X variable.
The output "ABCD $1" is not abcd123456789, because $1 is not considered as a reverse reference here.
*/
// -->
</SCRIPT>

6. foresight

Code
<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

Code
<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

Code
<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.

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.