Regular expression concepts:

This section discusses the use of regular expressions in JS. Note that the regular object is a reference type.

Initialization: literals and objects used

As with object definitions, regular expressions can be defined using literals or regular objects. The difference is the escape used.

When we use literals, we use / / to wrap the regular expression, and we can say match pattern or not write, not write. By default, it is not global matching. When we use literals, we usually use literal initialization, because it is easier and escape is not as complicated as string.

Var pattern1 = /at/g; Var pattern2 = RegExp('/at'.'g');
Copy the code

The following need to be escaped:

Properties of the re object

Since the properties of the regular object can be seen in literal variables, these methods are not very useful to know:

Exec () method (capture method)

The result of the exec method is not all matched items, but the first matched item, whose return value is shown in the following code.

// execVar text ='mom and dad and baby'; var pattern = /mom( and dad( and baby)?) ? /gi; var matches = pattern.exec(text); alert(matches.index); Alert (matches. Input); // 0 // has the index attribute to identify the position of the match in the string. Alert (matches[0]); alert(matches[0]); //mom and dad alert(matches[1]); Alert (matches[2]); //and babyCopy the code

The exec method should pay attention to the effect of the global flag /g on the execution result. When the global flag /g is not written, it is not a global match. Each execution is started from the beginning, and the next match is continued from the previous one

// do not write /g each time matches var text = from the beginning'cat, bat, sat, fat'; var pattern1 = /.at/; var matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]); //cat alert(pattern1.lastIndex); //0 // the lastIndex method is the key, notice that it is the property of the object pattern1, which records where the re is matched. Matches = pattern1.exec(text); alert(matches.index); //0 alert(matches[0]); //cat alert(pattern1.lastIndex); / / 0Copy the code

Plus PI over g and 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 matches = pattern1.exec(text); //3 //lastIndex matches = pattern1.exec(text); alert(matches.index); Alert (matches[0]); //bat alert(pattern1.lastIndex); / / 8Copy the code

The test method

The test method returns true false after a match, if you only want to know if there was a match, but not the result. It is used in if statements to check whether user input is valid.

var text = '000-00-0000';
var pattern = /\d{3}-\d{2}-\d{4}/;

if (pattern.test(text)) {
alert('matched');  //matched
}
Copy the code

Global properties

The RegExp property is used to retrieve global RegExp information, such as what string was matched and what information was matched last time. This is a bit like a global static variable

Unsupported functions

More front-end dry goods and front-end interview materials to get scanned wechat: