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

Technology
©2019-2020 Toolsou All rights reserved,
TP6 Application examples of verifier and correct verification data ESP8266/ESP32 System : Optimize system startup time 2021 year 2 Chinese programming language ranking 2021 year 1 Monthly programmer salary statistics , average 14915 element CSS architecture design It's not depravity that's terrible , It's about knowing you're falling Gude Haowen serial - You deserve to be an engineer ( Preface ) Software testing BUG describe C Course design of language programming of 《 Student achievement management system 》vue In the project axios Global encapsulation of