1, is just to handle the string: matching, capturing
Match: Verify that the current string conforms to our rules (each one is a rule)
Capture: In the entire string, the characters that match the rules are sequentially fetched to the--->exec, match, replace
2, regular composition: meta-character, modifier
Metacharacters
Special meaning of the metacharacters:
\d matches a 0-9 number equivalent to [0-9], and it's the opposite of
\d matches an arbitrary character except 0-9 equivalent to ""
\w matches a 0-9, A-Z, a-z_ number or character, equivalent to [0-9a-za-z_],
\s matches a white space character (space, Tab ...). )
\b matches the boundary of a word "w100 w000"
\ t matches a tab
\ n matches a newline
. Matches an arbitrary character other than \ n
^ begins with a certain meta character
$ ends with a single character
\ translated characters
One of the x|y x or Y
[xyz] x, y, Z, any one of the
[^XYZ] In addition to X, Y, Z, any one of the
[A-z]-> matches any one character in a-Z
[^a-z]-> matches any one character except A-Z
() grouping in regular
Quantifiers:
* 0 to multiple
+ 1 to multiple
? 0 to 1
? There are more meanings in the regular
Placed behind a quantifier-less character represents 0-1 occurrences such as/^\d?$/0-9 Direct number 0 to 1 times
Placed behind a quantifier meta character, to cancel the capture when greedy/^\d+?$/capture is only the first captured number to obtain the "2015"--->2
(?:) Grouping value matching does not capture
(? =) forward check
(?!) Negative pre-investigation
() the Role
1) Change the default priority level
2) can be grouped capture
3) grouped references
{n} appears n times
{N,} appears n to multiple
{N,m} appears n to M times
Ordinary meta character
Any word regular in addition to the above has special significance, the other is the representative of their own meaning of ordinary meta-characters
Modifiers:
I: Ignore the case of letters
M:multiline Multiple Line matching
G:global Global Match
Regular regular use in the project
1 The regular of the effective number
Valid numbers mean: positive, negative, 0, decimal
Part I: There may be an increase or decrease or no
Part Two: One number can be 0, and the number of digits cannot begin with 0.
Part Three: You can have decimals or no decimals, but once a decimal point appears, at least one digit
var reg =/^[+-]? (\d| [1-9]\d+) (\.\d+) $/;
Valid positive integer (contains 0):/^[+]? (\d| [1-9]\d+) $/;
Valid negative integer (contains 0):/^-(\d|[ 1-9]\d+) $/;
Judge mobile phone number (simple version):
var reg=/^1\d{10}$/;
Judge the mailbox
The first part: numbers, letters, underscores,-one to many places
Part II: @
Part III: Numbers, letters, one to many places
Part IV: (. Two to four bits). com. cn. Net. . com.cn
var reg =/^[0-9a-za-z_-]+@[0-9a-za-z-]+ (\.[ a-za-z]{2,4}) {1,2}$/
To judge the age between 18 and 65.
18-19/20-59/60-65
var reg =/^ ((18|19) | ( [2-5]\d) | (6[0-5]) $/
True and effective People's Republic of China name 2-4-digit Chinese characters
var reg =/^[\u4e00-\u9fa5]{2,4}$/;
ID number
The top six is the province-> City-> County (district)
Four-year two-month two-day
Simple version
var reg =/^\d{17} (\d| X) $/;
130828199012040617
Complex version
var reg =/^ (\d{2}) (\d{4}) (\d{4}) (\d{2}) (\d{2}) (?: \ D{2}) (\d) (?: \ d| X) $/;
Detail Knowledge points
Any character appearing in it is representative of its own meaning, for example: [.] "." In the is to represent a decimal point instead of any character other than \ n
18 is not the number 18 but 1 or 8, for example [18-65] is either 1 or 8-6 or 5.
1, Exec Regular Capture method---> First match, and then capture the matching content
If the string does not match this regular, the captured return result is null
If it matches a regular match, the result returned is an array
Example
var str = "2015zhufeng2016peixun"
var reg =/\d+/;
The first item is what we capture.
Index: the indexed position at which the captured content begins in the meta string
Input: The original string captured
2, regular capture is lazy
The regular each capture starts with the lastindex value, the first time the capture, the lastindex=0, starting from the original string index of 0 to find the capture, and by default, the first capture completes, the lastindex value does not change, or 0, So the second capture starts with the original string index of 0 and finds the first captured content
Workaround: Add the Global modifier g---> Plus g, after the first capture completes, the value of the lastindex changes to the start index of the first character after the first capture, and the second catch is to continue looking backwards ...
Question: Do not use the global modifier g to manually modify the value of the lastindex after each capture is done?
No, although lastindex has been manually modified, it does change the value of lastindex, but the regular lookup starts with index 0.
var str = "zhufeng2015peixun2016";
var reg =/\d+/g;
Example
To prevent a dead loop that is not caused by a global modifier g, we add a G to the manual that does not add g before processing.
RegExp.prototype.myExecAll = function Myexecall () {
var _this = this, str = arguments[0], ary = [], res = NULL;
! _this.global? _this = eval (_this.tostring () + "G"): null;
res = _this.exec (str);
while (res) {
Ary[ary.length] = res[0];
res = _this.exec (str);
}
return ary;
};
var ary = Reg.myexecall (str);
Console.log (ary);
Console.log (Reg.lastindex);//->0
var res = reg.exec (str)
; Console.log (res);
Console.log (Reg.lastindex);//->11
res = reg.exec (str)
; Console.log (res);
Console.log (Reg.lastindex);//->21
res = reg.exec (str)
; Console.log (res);//->null
3. Match: There is a method called match in the capture string that can also be captured, and as long as we cancel the lazy nature of the regular, the match method can be used to capture all the content.
var str = "zhufeng2015peixun2016";
var reg =/\d+/g;
Console.log (Str.match (reg));
Question: Well, how about we replace exec with match?
4, regular packet capture
Each time capturing, not only can the big regular match content capture, but also can each small group (Zhong) matches the content to capture separately
var str = "zhufeng[2015]peixun[2016]";
var reg =/\[(\d) (\d+) \]/g;
var res = reg.exec (str);
Console.log (res);
["[2015]", "2", "015", Index:7, Input: "zhufeng[2015]peixun[2016"]
The first item is the content of the large regular capture res[0]
The second item is the content captured by the first group Res[1]
The third item is the content captured by the second grouping rex[2]
。。。。。
Only matches in groups are not captured: if we perform a match to the grouped content but do not capture, just precede the group with the following:
var str = "zhufeng[2015]peixun[2016]";
var reg =/\[(?: \ d) (\d+) \]/g;
var res = reg.exec (str);
Console.log (res);
["[2015]", "015" ...]
The first item in the array is the content of the large regular capture res[0]
The second item in the array is the content captured by the second group Res[1]
The first group adds?:, so only matches not caught
5, the difference between exec and match
Match can only catch large regular matching content, for packet capture, can not get the packet matching content, so if the capture does not need to capture the contents of the packet, we are more convenient to use match, if you need to capture the content of grouped, we can only use exec to capture
var str = "zhufeng[2015]peixun[2016]";
var reg =/\[(\d+) \]/g;
Console.log (Str.match (reg));//->["[2015]", "[2016]"]
var ary = [];
var res = reg.exec (str);
while (res) {
//ary.push (res[1]);
Ary.push (regexp.$1);//regexp.$1 gets the content that is currently captured by the first group (possibly not being captured in some IE browsers)
res = reg.exec (str);
Console.log (ary);
6, regular greed: at each time of capture, always follow the longest result of the regular match capture
var str = "zhufeng2015peixun2016";
var reg =/\d+/g;
Console.log (Reg.myexecall (str));//-->["2015", "2016"]
var str = "zhufeng2015peixun2016";
var reg =/\d+?/g;
Console.log (Reg.myexecall (str));//-->["2", "0", "1", "5", "2", "0", "1", "6"]
7. Group reference
\2 represents what appears to be exactly the same as the second group.
\1 represents the exact same content as the first group
var reg=/^ (\w) (\w) \2\1$/;
" Woow "," 1221 "...
8. String method---->replace: Replace a character in a string with a new one
1) without the use of regular
Performing a replace can only replace one of the strings, and you need to replace multiple instances of the same
var str = "zhufeng2015 zhufeng2016";
Zhufeng "->" "everest"
str = str.replace ("Zhufeng", "Everest"). Replace ("Zhufeng", "Everest");
Sometimes, even if you do it many times, you cannot achieve the replacement
"Zhufeng"-> "Zhufengpeixun"
str = str.replace ("Zhufeng", "Zhufengpeixun"). Replace ("Zhufeng", "Zhufengpeixun") );
[the first parameter can be a regular] that replaces all the regular matches (but is lazy by default, as with capture, and only with the global modifier G.)
var str = "zhufeng2015 zhufeng2016";
str = str.replace (/zhufeng/g, "Zhufengpeixun");
Console.log (str);
1 implementation and number of executions
Actually, it's exactly the same as the Exec capture principle.
For example: Our second parameter, if passed a function, is executed once in a string when the current function is captured once in a-> the two times in the argument, so the function was executed twice.
var str = "zhufeng2015 zhufeng2016";
str = str.replace (/zhufeng/g, function () {
2) Parameter Problem
Console.dir (arguments);
Not only perform functions, but also pass parameters to our function, and pass parameters that are identical to what each exec captures
If this is the first exec capture->["Zhufeng", Index:0,input: "Original string"]
The first execution of the parameters inside the function
Arguments[0]-> "Zhufeng"/**/
Arguments[1]-> 0 is equivalent to the index position at the start of index capture in exec
ARGUMENTS[2]-> "raw string" equivalent to the input in exec
3) return value problem
What return returns is the equivalent of replacing what is currently captured
return "Zhufengpeixun";
};
Console.log (str);