Regular Expression Summary
A regular expression is a text mode consisting of common characters (such as characters a to z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string.
The following section summarizes some knowledge points about regular expressions. The specific content is as follows:
1. metacharacters
[Metacharacters with special meanings]
\ D-> match a 0-9 number, which is equivalent to [0-9]. The opposite is \ D-> match any character except 0-9.
\ W-> matches a number or character of 0-9, A-z, a-Z, and _, which is equivalent to [0-9a-zA-Z _]
\ S-> match a blank character (space, tab ...)
\ B-> match the boundary of a word
\ T-> match a tab
\ N-> match a line feed
.-> Match any character except \ n
^-> Starts with a metacharacter.
$-> Ended with a Metacharacter
\-> Transfer Character
X | y-> one of x or y
[Xyz]-> Any one of x, y, and z
[^ Xyz]-> except for any character in xyz
[A-z]-> match any character in a-z
[^ A-z]-> match any character except a-z
()-> Group in the Regular Expression
Note:
1) about []
A. [+]-> all the characters in brackets represent their own meanings.
B. [12-65]-> This Is Not 12-65, but one of 1/2-6/5.
2) about ()
A. The role of grouping is to change the default priority, for example:/^ 18 | 19 $/, 181, 189, 119, 819... all match, not the 18 or 19 we think, but the change to/^ (18 | 19) $/is simply 18 or 19
B. You can capture the content that matches the big regular expression, and capture the content that matches the group.-> group capture
C. Group reference, for example:/^ (\ d) (\ w) \ 2 \ 1 $/. Here, \ 2 is the same as the content of the second group, \ 1 is the same content as the first group. For example, "0aa0" is the same.
[Number of metacharacters]
*-> 0 to multiple
+-> 1 to multiple
? -> 0 to 1
{N}-> appears n times
{N ,}-> appear n to multiple times
{N, m}-> appear n to m times
Note:
1) about? Of
A. placed after the non-quantifiers metacharacters, indicating 0-1 occurrences
B. placed after the keyword metacharacters, which indicates the greed when the capture is canceled, for example, reg =/\ d +/; reg.exe c ("2015 ") -> "2015", but if the regular expression writes reg =/\ d +? /; Reg.exe c ("2015")-> "2"
C. Add? :, Indicates that the current group does not capture a match, for example:/^ (? : \ D +) $/
D. Add? =, Forward pre-query, for example:/^ abcdef (? = 1 | 2) $/Only "abcdef1" and "abcdef2" Match
E. Add?! At the beginning of the group ?!, Negative pre-query, for example:/^ abcdef (?! 1 | 2) $/Except for "abcdef1" and "abcdef2", all others that match "abcdef (anything )"
[Metacharacters that represent their own meanings]
In addition to the above, any character we appear in the literal mode represents our own meaning.
Var num = 12;
Var reg =/^ \ w "+ num +" $/;-> here "+ num +" is not to splice the value of the variable, but both "and + are metacharacters.
-> To splice strings and variables, you can only create regular expressions using instances.
2. Modifier
I-> ignoreCase ignores uppercase and lowercase letters
G-> global match (the addition of g can solve the laziness during regular expression capturing)
M-> multiline multi-line Matching
3. Common Regular Expressions in projects
1) Valid numbers
var reg=/^[+-]?(\d|([1-9]\d+))(\.\d+)?$/;
2) email
var reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
3) telephone
var reg = /^1\d{10}$/;
4) age between 18 and 65
var reg = /^((18|19)|([2-5]\d)|(6[0-5]))$/;
5) Chinese name
var reg = /^[\u4e00-\u9fa5]{2,4}$/;
6) ID card
Var reg =/^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2 })(? : \ D {2}) (\ d )(? : \ D | X) $/; //-> 12828 (province, city, and county) 1990 (year) 12 (month) 04 (day) 06 1 (odd number is male even number is female) 7 (number or X)
4. Regular Expression matching
Reg. test ([string])-> true indicates that the matching is successful. false-> the matching is unsuccessful.
5. Regular Expression capture
1)reg.exe c ([string])
-> First, match is performed. If the match succeeds, it is captured and an array is returned. If the match fails, null is returned;
-> Regular Expression capturing is both lazy and greedy.
-> Solve laziness by adding the global modifier g at the end of the regular expression
-> To solve greed, add?
The above is a summary of the regular expression provided by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!