Before starting regular expressions, here's a question for everyone to think about and see what solutions they have.
Problem: Calculating the length of a string (a double-byte character length meter 2,ascii character 1)
var str = "Chinese character a BC;";
All right, come on! We start JS Regular expression
Here is the basic knowledge of the JS expression should be mastered simple list:
<1> I, M, g
<2> ^, $, \w, \d, \s, \b,.
<3> *, +,?, {n}, {n,}, {n,m}
<4> test, Exac, match, search, replace, split
///////////////////////////////////////////////////
<1>
I: Case insensitive
M: Multi-line matching
G: Global Match
<2>
^: first match
$: Tail match
\w: Letters, numbers, underline matches
\d: Digital Match
\s: Blank Match
\b: Word-to-tail matching
.: Any character except line break
<3>
*: Match 0 or more of previous characters
+: Match 1 or more of previous characters
?: Match previous character 0 or 1
{n}: match the previous character N times
{N,}: match the previous character at least n times
{n,m}: match the previous character N times to M times
<4>
Test:reg.test (str); Back to True/false
Exec:reg.exec (str); Returns the first matched value as an array
Match:str.match (REG); Returns an array
Search:str.search (REG); return number
Replace:str.replace (REG); return string
Split:str.split (REG); Returns an array
For ease of understanding, here are the various matching patterns, followed by 6 ways
One, the matching mode and the regular symbol of the detailed
1. Simple mode (matched only by a combination of ordinary characters)
var reg =/ab0c/;
2. Symbols ^
Match the starting end of a string
3. Symbol $
Matches the end of a string
4. Symbols *
Match a previous character 0 or more times
5. Symbol +
Match a previous character 1 or more times
6. Symbols?
The usage is special, in general it is used to do 0 or 1 matches to the previous character,
But it has two other special uses:
If immediately after *, +,?, and {}, indicates the minimum number of matches for the original match
7. Symbols.
Match any single character, except for line break
8. Symbol (x) x refers to a string that matches X and remembers X
var regx=/a (b) C/;var rs=regx.exec ("ABCDDABCD"); Console.log (RS);
as can be seen from the above,/a (b) c/matches "abc" in "Abcddabcd", because () the reason, B will also be recorded,
So the contents of Rs return are: {abc,b}
9. Symbol (?: x) x refers to a string match x, does not remember X
var regx=/a (b) C/;var rs=regx.exec ("ABCDDABCD"); Console.log (RS);
As can be seen from the above,/a (b) c/matches "abc" in "Abcddabcd" because (?:) The reason, B will not be recorded,
So the contents of Rs return are: {ABC}
10. The symbol x (? =y) matches x only if it is immediately followed by Y. X will be remembered, Y will not be remembered.
var regx=/user (? =name)/;var rs=regx.exec ("The username is Mary");
Result: The match was successful, and the value of RS is {user}
11. Symbol x (?! y) matches x, only if the following is not followed by Y. X will be remembered, Y will not be remembered.
var regx=/user (?! Name)/;var rs=regx.exec ("The user name is Mary");
Result: The match was successful, and the value of RS is {user}
12. Symbol X|y match x or y if x and y match up, then just remember X
var regx=/beijing|shanghai/;var rs=regx.exec ("I Love Beijing and Shanghai");
Result: The match succeeded, the value of RS is {Beijing}, although Shanghai also matches, but will not be remembered.
13. Symbol {}
{n} matches the first character n occurrences
{N,} matches the previous character at least n occurrences
{N,m} matches the previous character at least n times the occurrence of a maximum of M times
14. Symbol [XYZ] XYZ represents a string
Represents a character in a match [], [XYZ] is equivalent to [x-z]
15. Symbol [^XYZ] xyz represents a string
Represents a character in a match [], [^xyz] is equivalent to [^x-z]
16. Symbol \d matches a numeric character, equivalent to [0-9]
var regx =/user\d/;var rs = regx.exec ("user145"); Console.log (RS);
Result: The match succeeded, the value of RS is: ["user1", index:0, Input: "user145"]
var regx =/user[0-9]+/;var rs = regx.exec ("user145"); Console.log (RS);
Result: The match succeeded, the value of RS is: ["user145", index:0, Input: "user145"]
Notice the difference between the two
17. Symbol \ r matches a carriage return character
18. Symbol \s matches a space character
19. Symbol \w matches a number, _, or letter table character equivalent to [a-za-z0-9_]
20. Symbol \XHH matches a character represented by a two-bit 16 decimal number
21. Symbol \uhhhh matches the character expressed by a four-bit 16-digit number
22. Symbol [\U4E00-\U9FA5] matches Chinese characters
23. Symbol [^\X00-\XFF] matches double-byte characters
Second, matching method
<1> Test () Method:
Reg.test (str); Back to True/false
var str = "12abc678abc2hh"; var reg =/abc/;console.log (reg.test (str)); Truereg =/rgy/;console.log (reg.test (str)); False
////
<2> Exec () method:
Reg.exec (str); Returns the first matched value as an array
var str = "12abc678abc2hh"; var reg =/abc/;console.log (reg.exec (str)); ["ABC", INDEX:2, Input: "12abc678abc2hh"]
////
<3> Match () method:
Str.match (REG); Returns an array
If there is only one match, the-->[matched string is returned, the first matching index,input]
If more than one is matched, an array of multiple matching strings is returned, and the following example makes sense.
var str = "12abc678abc2hh"; var reg =/abc/;console.log (Str.match (reg));//["ABC", INDEX:2, Input: "12abc678abc2hh"]var re g =/abc/g;//Global match console.log (str.);//["ABC", "ABC"]
////
<4> Search () method:
Str.search (REG); return number
var str = "12abc678abc2hh"; var reg =/abc/;console.log (Str.search (reg)); 2var reg =/abc/g;console.log (Str.search (reg)); 2
visible Search () matches only the first one and returns its index
////
<5> replace () method:
Str.replace (reg);//return string
var str = "12abc678abc2hh"; var reg =/abc/g;console.log (Str.replace (Reg, "abc"));//12ABC678ABC2hhconsole.log ( Str.replace (/(\d+) ([a-z]+)/g, "$2[$1]"));//abc[12]abc[678]hh[2]
The latter is a more advanced usage and needs to be well understood.
(\d+) and ([a-z]+) represent two domains respectively, corresponding to $ and $, the others are good to understand.
////
<6> Split () method:
Str.split ();//Returns an array formed from a spacer partition
var str = "12abc678abc2hh"; Console.log (Str.split (/[a-z]+/));//["12", "678", "2", ""]
This demonstrates the process of forming an array as a string interval, and also shows the power of regular Expressions .
/////////////////////////////////////////////////////////////////////////
Well, I think you've got your own thoughts about the questions you started, and here's a way to solve them with regular expressions.
String.prototype.len = function () {return this.replace (/[^\x00-\xff]/g, "AA"). Length; var str = "Chinese character a BC;"; Console.log (Str.len ());//9
JS Regular expression