regular expression

<> concept

This section talks about regular expressions in js Use in , Note that the regular object is a reference type .

<> initialization : Literal and object usage

As defined by the object , We can define regular expressions with literals , You can also use regular objects to define regular expressions , The difference between them is that there are some differences in the use of escape .

When using literal , use /
/ Wrap regular expressions , It can be followed by a matching pattern or not , Do not write default is not a global match , When we use it , It's usually initialized with literals , Because it's easy , Escape is not as complicated as string .
// regular expression /at/g Match all at /g Represents a global search var pattern1 = /at/g; // Note that strings are not used var pattern2
= RegExp('/at', 'g');
These need to be escaped :

<> Properties of regular objects

Because the properties of regular objects can be seen in literal variables , So these methods are not very useful , Just know :

<>exec() method ( Capture method )

exec The result of method execution is not all matches , It's the first item to match , His return value is shown in the following code ( About the concept of capture group , Let's talk about it later , Here we know that only one will match at a time , In the first 0 Location return )
// exec Return value of var text = 'mom and dad and baby'; var pattern = /mom( and dad(
and baby)?)?/gi; var matches = pattern.exec(text); alert(matches.index); // 0
// have index Property identifies the position in the match string alert(matches.input); // mom and dad and baby //
Identify the original string alert(matches[0]); //mom and dad // Identity matching string alert(matches[1]); //and
dad // Capture string for group match alert(matches[2]); //and baby
exec Method should pay attention to global flag /g
Impact on execution results , Don't write /g When , Not a global match , Every execution starts from scratch , It's written /g, Matching is global , The next match is to continue from the previous one , See the following example to illustrate this situation
// Don't write /g Every time it's a match from scratch var text = 'cat, bat, sat, fat'; var pattern1 = /.at/; var
matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]);
//cat alert(pattern1.lastIndex); //0
//lastIndex Method is the key to the problem , Notice that it's pattern1 Object properties , Actually, it records where the regular matches , It's all here 0 The description is always match from zero
matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]);
//cat alert(pattern1.lastIndex); //0
add /g Let's see what happens
var text = 'cat, bat, sat, fat'; var pattern1 = /.at/g; var matches =
pattern1.exec(text); alert(matches.index); //0 alert(matches[0]); //cat
alert(pattern1.lastIndex); //3 //lastIndex The logo went through this match , Where should the next match start matches =
pattern1.exec(text); alert(matches.index); //5 // Identify where to match alert(matches[0]);
//bat alert(pattern1.lastIndex); //8
<>test method

test Method to return after matching true false, If you just want to know if there's a match , Don't want to know the exact result , Just use this . Commonly used in if In statement , Judge whether the user input is valid, etc .
var text = '000-00-0000'; var pattern = /\d{3}-\d{2}-\d{4}/; if
(pattern.test(text)) { alert('matched'); //matched }
<> Global properties

adopt RegExp Properties of , We can get some global regular information , For example, what string just matched the input , What information was last matched , It's kind of like a global static variable , Let's see what information global properties support

Note that global properties can be obtained by using both long and short property names . Let's take a look at the use of long and short attributes

<> Unsupported features

©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement