Home>

Regular expressions are widely used,This article collects and organizes some regular expression knowledge points to interpret and judge valid numbers, mobile phone number mailbox regular expression,Friends who are interested in common knowledge about regular expressions

1. Regularity is only used to process strings:match, capture

Match:verify that the current string conforms to our rules (every regular is a rule)

Capture:In the entire string,Get the characters that meet the rules in order --->exec, match, replace

2. Regular composition:metacharacters and modifiers

Metacharacters:

Special meaning metacharacters:

\ d matches a number from 0-9 which is equivalent to [0-9], and the opposite

\ d Matching any character except 0-9 is equivalent to []

\ w matches a number or character from 0-9, a-z, a-z_,Equivalent to [0-9a-za-z_],

\ s matches a blank character (space, tab ...)

\ b matches a word boundary "w100 w000"

\ t matches a tab

\ n matches a newline

. Matches any character except \ n

^ Starts with a metacharacter

$Ends with a metacharacter

\ Transpiled characters

x | y x or y

[xyz] any of x, y, z,

[^ xyz] except for any of x, y, z,

[a-z]->matches any character in a-z

[^ a-z]->matches any character except a-z

() Grouping in regular

quantifier:

* 0 to multiple

+ 1 to more

?0 to 1

?More meaning in regular

Placed behind a non-quantifier meta-character represents 0-1 occurrences, such as/^ \ d?$/appears 0-9 directly the number 0 to 1

After a quantifier metacharacter,Cancel greed when capturing/^ \ d +?$/When capturing, just get the first captured number "2015" --->2

(?:) Grouped value matches are not captured

(?=) Forward Preview

(? ;!) Negative Preview

The role of ()

1) Change the default priority

2) Can capture packets

3) group reference

{n} appears n times

{n,} appears n or more times

{n, m} appears n to m times

Ordinary metacharacter

Any character except the above has a special meaning in the regular,The others are ordinary metacharacters representing their own meaning

Modifier:

i:ignore case

m:multiline

g:global

Regulars used in projects

1) Judgment is a regular number

Significant numbers are:positive, negative, zero, decimal

Part I:Addition, Subtraction or No

Part 2:One digit can be 0, and multiple digits cannot start with 0

Part III:There may or may not be decimals,But once the decimal point appears,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 +) $/;

Judging the phone number (simple version):

var reg =/^ 1 \ d {10} $/;

Judge Mailbox

Part 1:Numbers, letters, underscores,-one to many digits

the second part:@

Part III:Numbers, letters, one or more digits

Part 4:(.two to four digits) .com .cn .net .. .com.cn

var reg =/^ [0-9a-za-z _-] [email protected][0-9a-za-z-] + (\. [a-za-z] {2,4}) {1,2} $/

Judging the age of 18 to 65

18-19/20-59/60-65

var reg =/^ ((18 | 19) | ([2-5] \ d) | (6 [0-5])) $/

Real and valid Chinese name 2-4 characters

var reg =/^ [\ u4e00- \ u9fa5] {2,4} $/;

identification number

The top six are provinces->cities->counties (districts)

Four years two months two days

Simple version

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) $/;

Detail knowledge

Any characters appearing in it represent their own meaning,For example:"." In [.] Means a decimal point instead of any character except \ n

18 in it is not the number 18 but 1 or 8. For example, [18-65] is any of 1 or 8-6 or 5.

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

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

If it matches the regular,The result returned is an array

example

var str="=" 2015zhufeng2016peixun "

var reg =/\ d + /;

The first is what we capture

index:the index position of the captured content starting in the meta string

input:the captured raw string

2.Regular capture is lazy

Every regular capture starts with the lastindex value. When the first capture,lastindex=0, search and capture from the position where the original string index is 0,And by default,The first capture is complete,The value of lastindex has not changed.Still 0, so the second capture still starts from the original string index 0,It ’s the first time I found it.

Solution:Add the global modifier g --->After adding g, after the first capture is completed,The value of lastindex has changed,Becomes the start index of the first character after the first capture,The second capture is to search backwards...

Question:Isn't it possible to manually modify the value of lastindex after each capture without using the global modifier g?

No, although the lastindex was manually modified, it did change the value of lastindex.But the regular search starts from index 0

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

example

To prevent the infinite loop caused by not adding the global modifier g,Before we deal with it,For those who do not add g, 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:There is a method called match in the capture string that can also achieve capture.And as long as we remove the regular laziness,Once you execute the match method, you can capture everything

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

Question:Then how good are we to replace exec with match?

4.Regular packet capture

Every time I capture,Not only can you capture the content of big regular matches,And you can also capture the content of each small group (sub-regular) match 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 captured by the Great Regular res [0]

The second item is the content captured by the first packet res [1]

The third item is the content captured by the second packet rex [2]

. . . . .

Grouping only matches without capturing:If we perform matching of the grouping content but do not capture,Just add? ;:in front of the group

 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 large regular res [0]

The second item in the array is the content captured by the second group res [1]

The first grouping was added with?:, so only matches were not captured

5, the difference between exec and match

match can only capture the contents of a large regular match,For packet capture,It is not possible to get content that matches the group.So if you do n’t need to capture the content of the packet during capture,We use match directly, if you need to capture the content of the packet,We can only use exec to capture 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.exec (str);
  while (res) {
    //ary.push(res[1]);
    ary.push (regexp. $1);//regexp. $1 gets the content captured by the first packet of the current regular,(May not capture values ​​under some browsers)
    res=reg.exec (str);
  }
  console.log (ary);

6. Regular greed:at every capture,Always follow the longest result of a regular match

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" ]

Grouping references

\ 2 represents exactly the same content as the second group

\ 1 stands for exactly the same content as the first group

var reg =/^ (\ w) (\ w) \ 2 \ 1 $/;
  "woow", "1221" ...
</pre>
</div>
<p>
8. String method ---->replace:replace a character in a string with new content
</p>
<p>
1) Without using regularity
</p>
<p>
Performing a replace can only replace one of the strings,Need to replace multiple also need to execute multiple times
</p>
<div>
<pre>
var str="zhufeng2015 zhufeng2016";
  "zhufeng"->"Everest"
  str=str.replace ("zhufeng", "Everest"). replace ("zhufeng", "Everest");
</pre>
</div>
<p>
Sometimes even if executed multiple times,Can't achieve replacement
</p>
<div>
<pre>
  "zhufeng"->"zhufengpeixun"
  str=str.replace ("zhufeng", "zhufengpeixun"). replace ("zhufeng", "zhufengpeixun");

[The first parameter can be a regular] Replace all the content that matches the regular (but as the capture is lazy by default,(Only with the global modifier g)

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

1) The issue of execution and execution times

In fact, the principle is the same as that of exec capture.

For example:if our second parameter is passed as a function,This function is executed once every time the regex captures the current one in a string->In this question, a total of two are captured,So the function is executed twice

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

2) Parameter problem

console.dir (arguments);

Not only execute the function, but also pass parameters to our function,And the parameters passed are exactly the same as what is captured by each exec

If this is the first exec capture->["zhufeng", index:0, input:"raw string"]

Parameters in the first execution of the function

arguments [0]->"zhufeng"/** /

arguments [1]->0 is equivalent to index in exec starting to capture the index position

arguments [2]->"raw string" equivalent to input in exec

3) return value problem

What does return return,Is equivalent to replacing the currently captured content with

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

Related articles

  • Previous Analysis of JS using eval to parse JSON
  • Next Complete example of consistent hash algorithm implemented in PHP