preface

I haven’t updated my article for a long time recently, because I have been writing some interview questions recently, and then I have no time to write notes, because writing notes really consumes my energy. I am a little obsessive in learning, and I still hope to refine and clarify all the knowledge involved. Of course it was too obscure and felt useless so I automatically filtered it hahahahaha. I recently got a job. So relatively speaking, free time, of course, there are still several interviews have not finished. But I want to see if there’s a better option. Although I feel that the opportunity is not very big, after all, it is a social recruitment, it is estimated that the difficulty of the next few will be very high. Just do your best. The written examination of the public also came down, although the written examination is the second, but the interview of the public examination… Not for me, because I’m really bad at expressing myself. You know, if you want to get into it, you might have to sign up for 20 grand. Kill me. So I decided to give it up. I thought I’d be more interested in technology. However, it is also a kind of experience, any experience in life is a wealth. Finally, I would like to express my heartfelt thanks to 2020. The extraordinary year has made me mature faster.

Definition of a regular expression

In JS, regular expressions are defined in two forms.

  • Re the form of a literal

var Reg = /abc/gi

  • Creating an instance through the RegExp constructor takes two forms
    1. new RegExp(String[,String])

    In this form, one thing to note is that String does not contain a double slash, and second, String does not contain a double slash\Is to be escaped:

    Var a = new RegExp (" a [0 to 8] \ s ", "I") / / errors, the \ here is need to escape the var a = new RegExp (" a [0 to 8] \ \ s ", "gi") / / correct

    Here the second argument, String, is the next modifier. 2. New RegExp(regular literal [,String])

    This is nothing to say, pretty stupid this way.

    var a = new RegExp(/a[0-8]\s/,"gi")

Common special characters in regular expressions

  • Start operator ^
  • Terminator $

The start and end character specifies that a regular expression can match only strings that start and end.

var Reg = /abc/; Reg.test("abc"); //true Reg.test("aaabccc"); //true var Reg = /^abc$/; Reg.test("abc"); //true Reg.test("aababccc"); //falseCopy the code
  • Characters?

This means that the number of occurrences of a character expression is 0-1.

  • Character +

That means the number of occurrences of a character expression is 1- infinity

  • The characters *

That means the number of occurrences of a character expression is 0 to infinity

  • Character \ d

So it’s all numbers, 0 minus 9. Equivalent to [0-9]

  • Character \ D

That is, all non-numeric characters are included. Equivalent to [^ 0-9]

  • Character \ w

That is, all numbers, letters and underscores. Equivalent to [0-9 – a zA – Z_]

  • Character \ W

That means no alphanumeric underscores. Equivalent to [^ 0-9 a – zA – Z_]

  • Character \ s

Matches all null characters, tabs, newlines, Spaces, etc.

  • Character \ S

Matches all non-null characters.

A common modifier for regular expressions

  • g

Global matching, matching all items that match conditions.

  • i

Case insensitive. (ignore)

Regular expression method

The test method

Regexp. test(String)//true/false This method is very simple. Is to determine whether a string matches a regular expression. Returns true if there was a match, false if there was no match.

var Reg = /abc/; Reg.test('abcccc'); //true Reg.test('acccc'); //falseCopy the code

The exec method

This method is more complicated and can be divided into two cases.

RegExp.exec(String);
Copy the code

First, the exec method returns an array if it matches. Returns null if no match is found. And, this array comparison is special in that it has three additional attributes:

  • input

And this is the string that matches.

  • index

Is the index index of the matched string.

  • groups

This property refers to the concept of a named capture group.

Capture group

Capture group, in fact, in regular expression, I understand that a small bracket represents a capture group. For instance

var Reg = /(a)(b)(c)/; // There are three capture groups. A, b, c.Copy the code

What happens when you capture groups?

Capturing groups results in something like this:

The first element of the array, at index:0, is the matched string. The second element, at index:1, is the substring matched by the first capture group. And so on. This is what it looks like:Order of capture groups:

The order in which the groups are captured is essentially the order in which the left parentheses appear.

Named capture group

You may have noticed that there is a groups attribute that is always equal to undefined. This is because the content is used to display the named capture group.

How a named capture group is named
(? < capture group name > AAA)Copy the code

He’s going to group thisobjectDisplays the matching information for this named capture group, with the attribute name being the capture group name. Property value is the string that captures the group match.

Two cases of global and non-global matching of regular expressions

As I mentioned earlier, this exec method is special in that it is divided into regular expression global matching and non-global matching cases. In fact, there is no difference between the two cases.

The only difference is that, in the case of an imperfect match, it always returns the result of the first matched object. And global matches are going to go all the way back. Return null if not found

In essence, global matching sets the lastIndex of the regex to the next index of the last character in the string that is matched. So it will always match the first substring that meets the requirement, while a global match will match all the way down, and when it doesn’t match a string that meets the requirement, it will return null and set lastIndex to 0. A thousand words are not worth a graph. Global matching:No global match:LastIndex, that’s where every match starts.

So that’s pretty much the basics of regular objects. After reviewing the common methods of strings, I’ll summarize how strings and RegExp relate to each other.