Interpretation of common Regular Expressions knowledge points and interpretation of valid numbers, mobile phone number mailbox regular expressions, and regular expression Interpretation

Source: Internet
Author: User

Interpretation of common Regular Expressions knowledge points and interpretation of valid numbers, mobile phone number mailbox regular expressions, and regular expression Interpretation

1. Regular Expressions are only used to process strings: matching and capturing

Match: verify whether the current string meets our Rules (each regular expression is a rule)
Capture: In the entire string, the characters that comply with the rules are obtained in sequence ---> exec, match, replace

2. Regular Expression composition: metacharacters and modifiers

Metacharacters:

Special metacharacters:

\ D matches a 0-9 number, which is the opposite of a 0-9 number.
\ D matches any character except 0-9, which is equivalent 【]
\ W matches a number or character of 0-9, a-z, A-Z _, equivalent to [0-9a-zA-Z _],
\ S matches a blank character (space, tab ...)
\ B matches the boundary of a word "w100 w000"
\ T matches a tab
\ N matches a line feed.
. Match any character except \ n
^ Starts with a metacharacter.
$ Ends with a metacharacter.
\ Translated characters
X | one of y x or y
[Xyz] Any one of x, y, z
[^ Xyz] Except for any one of x, y, z
[A-z]-> match any character in a-z
[^ A-z]-> match any character except a-z
() Group in the Regular Expression

Quantifiers:

* 0 to multiple
+ 1 to multiple
? 0 to 1

? There are many meanings in regular expressions.

After a non-quantifiers metacharacters, 0-1 times are displayed, for example,/^ \ d? $/0-9 Direct display count 0 to 1

Put it behind the metacharacter of a quantizer and cancel the greedy character when capturing/^ \ d +? $/Only get the first number to be captured "2015" ---> 2
(? :) Group value matching is not captured
(? =) Forward pre-Query
(?!) Negative pre-Query

() Function
1) Change the default priority
2) group capture is supported.
3) Group Reference

{N} appears n times
{N,} appears n to multiple times
{N, m} appears n to m times

Common metacharacters

Except for the above special meanings, any character in the regular expression is a common metacharacter that represents its own meaning.

Modifier:

I: Ignore uppercase/lowercase letters
M: multiline multi-line Matching
G: global match

Regular Expressions frequently used in projects

1) judge to be a regular expression of valid numbers

Valid number indicates positive number, negative number, zero number, and decimal number.

Part 1: addition or subtraction may occur or
Part 2: one digit can be 0, and Multiple Digits cannot start with 0
Part 3: there can be decimal places or decimal places, but once there is a decimal point, it must be followed by at least one digit.
Var reg =/^ [+-]? (\ D | [1-9] \ d +) (\. \ d + )? $ /;

Valid positive integer (including 0):/^ [+]? (\ D | [1-9] \ d +) $ /;

Valid negative integer (including 0):/^-(\ d | [1-9] \ d +) $ /;

Determine the mobile phone number (simple version ):
Var reg =/^ 1 \ d {10} $ /;

Determine email
Part 1: numbers, letters, underscores,-one to Multiple Digits
Part 2 :@
Part 3: numbers, letters, one to Multiple Digits
Part 4: (. Two to four digits). com. cn. net.. .com.cn
Var reg =/^ [0-9a-zA-Z _-] + @ [0-9a-zA-Z-] + (\. [a-zA-Z] {2, 4}) {1} $/

Determine the age between 18 and 65
18-19/20-59/60-65
Var reg =/^ (18 | 19) | ([2-5] \ d) | (6 [0-5]) $/

Authentic and valid name of the People's Republic of China 2-4 Chinese Characters
Var reg =/^ [\ u4e00-\ u9fa5] {2, 4} $ /;

ID card number
The first six are provinces, cities, and counties (districts)
Four-digit, two-month, two-day

Simple Edition

Var reg =/^ \ d {17} (\ d | X) $ /;
130828199012040617

Complex Edition

Var reg =/^ (\ d {2}) (\ d {4}) (\ d {4}) (\ d {2}) (\ d {2 }) (? : \ D {2}) (\ d )(? : \ D | X) $ /;

Detailed knowledge point

Any character in it represents its own meaning. For example, "." In [.] represents a decimal point, not any character except \ n.
18 is not 18, but 1 or 8. For example, [18-65] is 1, 8-6, or 5.

1. exec regular capture method ---> first match, then capture the Matching content

If the string does not match this regular expression, the returned result is null.

If it matches the regular expression, the returned result is an array.

Example
Var str = "2015zhufeng2016peixun"
Var reg =/\ d + /;

The first one is what we capture

Index: the position of the index where the captured content starts in the meta string.
Input: the captured original string.

2. Regular Expression capturing is lazy.

Each RegEx capture starts from the value of lastIndex. During the first capture, lastIndex = 0 searches for the capture from the position where the original string index is 0. By default, after the first capture is completed, the value of lastIndex has not changed, but it is still 0. Therefore, the second Capture still starts from where the index of the original string is 0. In this way, the content captured for the first time is found.
Solution: after adding the global modifier g ---> with g, after the first capture is complete, the value of lastIndex changes to the start index of the first character after the content is captured for the first time, the second capture is to continue searching backward...
Q: Can I manually modify the value of lastIndex after each capture without the global modifier g?
No. Although lastIndex is manually modified, the value of lastIndex is indeed changed, but the regular query still starts from index 0.

var str = "zhufeng2015peixun2016";  var reg = /\d+/g;

Example

To prevent the endless loop caused by the absence of the global modifier g, we manually add a g

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: a method called match in the capture string can also be captured. If we cancel the laziness of the regular expression, we can execute a match method to capture all the content.

  var str = "zhufeng2015peixun2016";  var reg = /\d+/g;  console.log(str.match(reg));

Q: How nice is it to replace exec with match?

4. Grouping and capturing of Regular Expressions

During each capture, you can not only capture the content that matches the big regular expressions, but also capture the content that matches each small group (sub-regular ).

    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 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 group, rex [2].
.....

Group matching only does not capture: if we want to match the group content but do not capture it, we only need to add? :

  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 captured by the big Regular Expressions res [0]
The second item in the array is the content captured by the second group res [1]
The first group is added? :, So only matching does not capture

5. Differences between exec and match

Match can only capture content that matches a large regular expression. In group capturing, the content that matches a group cannot be obtained. Therefore, if you do not need to capture the content of a group, match is more convenient. If you need to capture the content of the group, you can only use exec to capture the content one by one.

Var str = "zhufeng [2015] peixun [2016]"; var reg =/\ [(\ d +) \]/g; // console. log (str. match (reg); //-> ["[2015]", "[2016]"] var ary = []; var res = reg.exe c (str ); while (res) {// ary. push (res [1]); ary. push (RegExp. $1); // RegExp. $1 get the content captured by the first group of the current regular expression (may not be captured in some ie browsers) res = reg.exe c (str);} console. log (ary );

6. Regular Expression greed: during each capture, the longest result of regular expression matching is always captured.

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 indicates the same content as the second group.

\ 1 indicates the 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 content.

1) when regular expressions are not used

One replace operation can only replace one of the strings. You must replace multiple replace operations.

Var str = "zhufeng2015 zhufeng2016"; "zhufeng"-> "Everest" str = str. replace ("zhufeng", "Everest "). replace ("zhufeng", "Everest ");

Sometimes it cannot be replaced even if it is executed multiple times.

  "zhufeng" -> "zhufengpeixun"  str = str.replace("zhufeng", "zhufengpeixun").replace("zhufeng", "zhufengpeixun");

[The first parameter can be a regular expression] replace all the content that matches the regular expression (but it is also lazy by default, only with the global modifier g)

    var str = "zhufeng2015 zhufeng2016";    str = str.replace(/zhufeng/g, "zhufengpeixun");    console.log(str);

1) execution and execution times

Actually, the principle of exec capture is exactly the same.

For example, if we use the second parameter to pass a function, this function is executed every time the regular expression captures the current function in the string.-> this topic captures this function twice in total, so the function is executed twice.

  var str = "zhufeng2015 zhufeng2016";  str = str.replace(/zhufeng/g, function () {

2) parameter issues

Console. dir (arguments );
It is not just about executing the function, but also passing parameters to our function, and the passed parameters are exactly the same as the content captured by each exec operation.
If it is the first exec capture-> ["zhufeng", index: 0, input: "original string"]
Parameters in the first execution function
Arguments [0]-> "zhufeng "/**/
Arguments [1]-> 0 is equivalent to the index location captured starting from the index in exec
Arguments [2]-> "original string" is equivalent to input in exec

3) Return Value Issues

What is returned by return is equivalent to replacing the content currently captured

   return "zhufengpeixun";  });  console.log(str);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.