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.
I used to send regular expressions for beginners without worry. Now I have added the Group to provide download improvements.
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.
two Javascript Regular Expressions are grouping knowledge
1) simple grouping
<script language = "JavaScript"> <br/> <! -- <Br/>/* simple grouping of Regular Expressions <br/> for example, we want to find the string mousemouse <br/> var recat =/mousemouse/GI; <br/> 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. <Br/> var recat =/(Mouse) {2}/GI; the brackets indicate that the column mouse appears twice in a row. <Br/> */<br/> var DATA = "ah-mousemouse"; <br/> var recat =/(Mouse) {2}/GI; <br/> var arrdata = data. match (recat); <br/> for (VAR I = 0; I <arrdata. length; I ++) <br/>{< br/> alert (arrdata [I]); <br/>}< br/> // --> <br/> </SCRIPT>
tip: you can modify some Code before running
2. Complex grouping
<Script language = "JavaScript"> <br/> <! -- <Br/>/* groups with complex Regular Expressions <br/>? Zero or one <br/> * zero or multiple <br/> + at least once or multiple times <br/> */<br/> var DATA = "BB ba da bad dad AA "; <br/> var recat =/([Bd] ad ?) /GI; // match ba da bad dad <br/> var arrdata = data. match (recat); <br/> for (VAR I = 0; I <arrdata. length; I ++) <br/>{< br/> alert (arrdata [I]); <br/>}< br/> // do not mind placing the group in the middle of the group <br/> // var Re =/(MOM (and Dad )?) /; Match Mom or Mon and DAA <br/> // --> <br/> </SCRIPT>
Tip: you can modify some code before running
3. Reverse reference
<textarea id="code2" rows="10" cols="95"><Script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression reverse reference */<br/> var stomatch = "#123456789"; <br/> var renumbers =/# (\ D +) /; <br/> renumbers. test (stomatch); <br/> alert (Regexp. $1); <br/>/* <br/> in this example, the pound following several or more numbers is matched, and groups numbers to store them. <br/>. After the test method is called, all reverse references are saved to the Regexp constructor <br/> from Regexp. $1 (it stores the first reverse reference). If there is a second reverse reference, it is <br/> Regexp. $2. If there is a third reverse reference, it is Regexp. $3. and so on. Because the group <br/> matches "123456780", this string is stored in Regexp. $1. <Br/> */<br/> var stochange = "1234 5678"; <br/> var rematch =/(\ D {4}) (\ D {4 }) /; <br/> var snew = stochange. replace (rematch, "$2 $1"); <br/> alert (snew); <br/>/* <br/> in this example, A regular expression has two groups, each of which has four numbers. In the second parameter of the Replace () method <br/>, $2 is equivalent to "5678", while $1 is equivalent to "1234 ", corresponding to the order in which they appear in the expression. <Br/> */<br/> // --> <br/> </SCRIPT></textarea>
Tip: you can modify some code before running
4 candidates
<script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression candidate */<br/> var stomatch1 = "red"; <br/> var stomatch2 = "black "; <br/> var rered =/red/; <br/> var reblack =/Black/; <br/> alert (rered. test (stomatch1) | reblack. test (stomatch1); <br/> alert (rered. test (stomatch2) | reblack. test (stomatch2); <br/>/* <br/> although the task can be completed, it is very long. Another way is to use the candidate operators of regular expressions. <Br/> */<br/> var stomatch1 = "red"; <br/> var stomatch2 = "black "; <br/> var reredorblack =/(Red | black)/; <br/> alert (reredorblack. test (stomatch1); <br/> alert (reredorblack. test (stomatch2 )); <br/> // --> <br/> </SCRIPT>
tip: you can modify some code before running it
5 Non-capturing Group
<textarea id="code4" rows="10" cols="95"><Script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression non-capturing group <br/> if you want to create a non-capturing group, you only need to add a question mark and a colon following the left parenthesis: <br/> */<br/> var stomatch = "#123456789 "; <br/> var renumbers = /#(? : \ D +)/; <br/> renumbers. test (stomatch); <br/> alert (Regexp. $1); <br/>/* <br/> the last line of code in this example outputs an empty string because the group is non-capturing, <br/> */<br/> var stomatch = "#123456789"; <br/> var renumbers = /#(? : \ D +)/; <br/> alert (stomatch. replace (renumbers, "ABCD $1"); <br/>/* <br/> for this reason, the Replace () method cannot pass Regexp. $ X variable to use any reverse reference. This Code <br/> outputs "ABCD $1" instead of "abcd123456789", because $1 is not considered as a reverse reference here. <Br/> */<br/> // --> <br/> </SCRIPT></textarea>
Tip: you can modify some code before running
6. foresight
<Script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression forward <br/> the forward direction is the same as its name, it tells the regular expression generator to look forward to some characters rather than moving the position <br/> */<br/> var stomatch1 = "bedroom"; <br/> var stomatch2 = "bedding "; <br/> var rebed =/bed (? = Room)/; <br/> alert (rebed. test (stomatch1); // true <br/> alert (rebed. test (stomatch2); // false <br/> // negative forward <br/> var stomatch1 = "bedroom"; <br/> var stomatch2 = "bedding "; <br/> var rebed =/bed (?! Room)/; <br/> alert (rebed. test (stomatch1); // false <br/> alert (rebed. test (stomatch2); // true <br/> // --> <br/> </SCRIPT>
Tip: you can modify some code before running
7 Border
<Script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression boundary <br/> ^ start of a row <br/> $ end of a row <br/> \ B word boundary <br/> \ B Non-word boundary <br/> */<br/> var stomatch = "important word is the last one. "; <br/> var relastword =/(\ W + )\. $/; <br/> relastword. test (stomatch); <br/> alert (Regexp. $1); // One <br/>/* <br/> 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 ($) <br/> */<br/> var stomatch = "important word is the last one. "; <br/> var relastword =/^ (\ W +)/; <br/> relastword. test (Stoma Tch); <br/> alert (Regexp. $1); // important <br/>/* <br/> in this example, the regular expression searches for one or more word characters after the start position of a row. If a non-word character <br/> matches to stop, important is returned. This example can also be implemented using word boundaries. <Br/> */<br/> var stomatch = "important word is the last one."; <br/> var relastword =/^ (. + ?) \ B/; <br/> relastword. test (stomatch); <br/> alert (Regexp. $1); // important <br/>/* <br/> here, regular expressions use inert quantifiers to specify that any character can appear before the word boundary, and can appear once or <br/> multiple times (if the greedy quantifiers are used, the expression matches the entire string ). <Br/> */<br/> var DATA = "first second thind fourth th Sixth"; <br/> var recat =/\ B (\ s + ?) \ B/g; <br/> var arrdata = data. match (recat); <br/> for (VAR I = 0; I <arrdata. length; I ++) <br/>{< br/> alert (arrdata [I]); <br/>}< br/>/* <br/> Use Word boundary to easily extract words from strings. <Br/> */<br/> // --> <br/> </SCRIPT>
Tip: you can modify some code before running
8 multi-row Mode
<textarea id="code7" rows="10" cols="95"><Script language = "JavaScript"> <br/> <! -- <Br/>/* Regular Expression multi-row mode <br/> you must specify the multi-row mode, only one word at the end of the row to be matched by the regular expression <br/> */<br/> var DATA = "first second \ n thind fourth \ n th Sixth "; <br/> var recat =/(\ W +) $/g; <br/> var arrdata = data. match (recat); <br/> for (VAR I = 0; I <arrdata. length; I ++) <br/>{< br/> alert (arrdata [I]); <br/>}< br/>/* <br/> only one word Sixth is returned. Because the linefeed blocks matching, only one word at the end of the row can be matched, <br/> Of course, you can also use the split () method to split strings into arrays, but you have to match each row separately. <Br/> in the past, I used to read a book but often read it in half. As a result, I used a lot of splits, in fact, it is very simple as the following <br/> example only requires the M parameter to perform multi-row matching. <Br/> */<br/> var DATA = "first second \ n thind fourth \ n th Sixth"; <br/> var recat =/(\ W +) $/GM; <br/> var arrdata = data. match (recat); <br/> for (VAR I = 0; I <arrdata. length; I ++) <br/>{< br/> alert (arrdata [I]); <br/>}< br/> // --> <br/> </SCRIPT></textarea>
Tip: you can modify some code before running
At this point, these are the basic methods of JavaScript Regular Expressions. If you see complicated regular expressions, you will suddenly feel open.
In addition, I know that some people are very lazy (including me), So I hereby package the example of Regular Expression grouping. Welcome to download and learn about JS Regular Expression grouping.