Second, JavaScript Regular Expressions are grouping knowledge.
1) Simple grouping
<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
<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
<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
<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
<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 of code in this example outputs 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
<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
<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
<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.