For a long time did not look at the regular expression, it happened to use today, restudying a book to learn it
50% of extrapolate practice in the original.
The basics of a JavaScript regular expression
1 JavaScript regular object creation and usage
declaring JavaScript regular expressions
Copy Code code as follows:
var recat = new RegExp ("cat");
You can also
var recat =/cat/; Perl Style (recommended)
2 learning the most commonly used test exec match search replace split 6 methods
1 Test checks to see if the specified string exists
Copy Code code as follows:
var data = "123123";
var recat =/123/gi;
Alert (recat.test (data)); True
Check to see if the character is present G continue to go down I case insensitive
2 Exec return query value
Copy Code code as follows:
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/i;
Alert (recat.exec (data)); Cat
3 Match Gets the query array
Copy Code code as follows:
var data = "123123,213,12312,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 Cat
}
4 Search to return to a location similar to IndexOf
Copy Code code as follows:
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.search (Recat)); 23
5 Replace replacement character using regular substitution
Copy Code code as follows:
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.replace (Recat, "libinqq"));
6) split using a regular split array
Copy Code code as follows:
var data = "123123,213,12312,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 under the simple class negative class range class combination class
Copy Code code as follows:
Simple class
var data = "1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ";
var recat =/[123]libinqq/gi;
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); 1LIBINQQ 2LIBINQQ 3LIBINQQ
}
Negative to Class
var data = "ALIBINQQ,1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ"; \u0062cf
var recat =/[^a123]libinqq/gi;
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); 4libinqq
}
Scope class
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 Class
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 JS is the most basic use of the method, see will not be copied to the notebook under the practice, see will be looked down.
Two JavaScript regular expressions are grouped knowledge
1) Simple grouping
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular Expressions Simple grouping
For example we are looking for string mousemouse
var recat =/mousemouse/gi;
Although this is possible, it is a bit wasteful. If you don't know how many times mouse in a string, what to do if you repeat it several times.
var recat =/(mouse) {2}/gi; The meaning column mouse of parentheses will appear 2 consecutive times on one line.
*/
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 groupings
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular Expressions complex groupings
? 0 times or once
* 0 or more times
+ at least one or more times
*/
var data = "BB ba da Bad dad AA";
var recat =/([Bd]ad?) /gi; Match out Ba da Bad dad
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}
And you don't mind grouping them in the middle of a group.
var re =/(Mom (and dad)?) /; Match out mom or Mon and DAA
-->
</script>
3 Reverse References
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular Expression Reverse Reference * *
var stomatch = "#123456789";
var renumbers =/# (\d+)/;
Renumbers.test (Stomatch);
alert (regexp.$1);
/*
This example attempts to match a pound followed by several or more digits, and groups the numbers
to store them. After calling the test method, all the reverse references are saved to the RegExp constructor
Starting from regexp.$1 (which holds the first reverse reference), if there is a second reverse reference, it is
Regexp.$2, if there is a third reverse reference exists, it is regexp.$3. And so on. Because the group
Matches the "123456780", so the string is stored in the regexp.$1.
*/
var stochange = "1234 5678";
var rematch =/(\d{4}) (\d{4})/;
var snew = stochange.replace (rematch, "$ $");
alert (snew);
/*
In this example, the regular expression has two subgroups, each grouped with four digits. The second argument in the Replace () method
, $ equals "5678" and "1234" corresponds to the order in which they appear in the expression.
*/
-->
</script>
4 candidates
Copy Code code as follows:
<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 accomplish the task, but very long, there is another way is the regular expression candidate operators.
*/
var sToMatch1 = "Red";
var sToMatch2 = "Black";
var reredorblack =/(Red|black)/;
Alert (Reredorblack.test (STOMATCH1));
Alert (Reredorblack.test (STOMATCH2));
-->
</script>
5 non-capture groupings
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular expression non-capture grouping
If you want to create a non-capture grouping, simply add a question mark and a colon followed by 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 not a catch,
*/
var stomatch = "#123456789";
var renumbers =/# (?: \ d+)/;
Alert (Stomatch.replace (renumbers, "abcd$1"));
/*
For this reason, the replace () method cannot use any of the reverse references through the REGEXP. $x variable, this code
The "abcd$1" of the output is not abcd123456789, because it is not considered a reverse reference here.
*/
-->
</script>
6 Outlook
Copy Code code as follows:
<script language= "JavaScript" >
<!--
* * Regular Expression Outlook
Foresight is just like its name, it tells the regular expression operator to look forward to some characters instead of moving the position
*/
var sToMatch1 = "bedroom";
var sToMatch2 = "Bedding";
var rebed =/bed (? =room)/;
Alert (Rebed.test (STOMATCH1)); True
Alert (Rebed.test (STOMATCH2)); False
Negative outlook
var sToMatch1 = "bedroom";
var sToMatch2 = "Bedding";
var rebed =/bed (?!) room)/;
Alert (Rebed.test (STOMATCH1)); False
Alert (Rebed.test (STOMATCH2)); True
-->
</script>
7 borders
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular expression bounds
^ Line Start
$ line End
\b The boundary of a word
\b A 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 a line, 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 finds one or more word characters after the start of the line. If you encounter a non-word character
Match stop, return Important. This example can also be implemented using a word boundary.
*/
var stomatch = "Important word is the last one."
var Relastword =/^ (. +?) \b/;
Relastword.test (Stomatch);
alert (regexp.$1); Important
/*
Here, the regular expression uses an inert quantifier to make any character before the word boundary, and can appear once or
multiple times (if you use greedy quantifiers, the expression matches the entire string).
*/
var data = "The second Thind fourth fifth sixth";
var recat =/\b (\s+?) \b/g;
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}
/*
Using word boundaries makes it easy to extract words from strings.
*/
-->
</script>
8 Multi-line mode
Code
Copy Code code as follows:
<script language= "JavaScript" >
<!--
/* Regular expression Multiline mode
To make a multiline pattern, just one word at the end of the line that the regular expression wants to match
*/
var data = "The second\n thind fourth\n fifth Sixth";
var recat =/(\w+) $/g;
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}
/*
The above only returns a word sixth, because the newline character blocks the match and can only match one word at the end of the line.
Of course, you can also use the split () method to split a string into groups, but you have to match each row separately.
Before not good reading often half-baked, look at half is still, resulting in a lot of split, in fact, very simple as the following
Examples require only the M parameter for multiple-line matching.
*/
var data = "The second\n thind fourth\n fifth Sixth";
var recat =/(\w+) $/gm;
var arrdata = Data.match (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}
-->
</script>
By the end, these are the basic methods of JavaScript regular expressions, and if you look at the complex, you will have a clear feeling.