Regular content in JavaScript isn’t much to talk about, but it takes some effort to fully understand it. I won’t go into all the details in this article. That would be a huge project. I’ll try to sort out some of the most common things you need to know about regex from the perspective of some of the most important concepts.

First, the basic concept of regularity

1.1 yuan character

Metacharacters are characters in regular expressions that have special meanings and cannot be used to represent themselves. For example, the asterisk does not represent the asterisk itself in the re, but matches any character. Common regular metacharacters are as follows.

\w matches word characters \W matches non-word characters \D matches numbers \D matches non-numbers \S matches Spaces \S matches non-spaces \B matches word boundaries \B matches non-word boundaries \n matches newlines \r matches carriage returns \ used to escape ^ matches the beginning of a string $ Matching the end of a stringCopy the code

1.2 character classes

A character class refers to a matching range built using the metacharacter ‘[]’. For example: / / abcde/g; Matches any of the characters abcde. /[^ ABC]/g; /[^ ABC]/g; Matches any character except ABC.

Quantifiers in re

The function of quantifiers is to match the number of repetitions of characters in a regular expression. There are mainly

? Zero or one matching character, at most one + One or more matching characters, at least one * any number of times, zero or countless {n,m} Matches characters that occur n to m times, including n and mCopy the code

Grouping in re

The metacharacter () is used in the re to group objects, and the contents of the objects can be captured and reused in the re.

let reg = /(\d{4})-\1/g;
Copy the code

For example, in the preceding regular expression, matching four digits in parentheses is a group, and the matching contents of this group can be used in the subsequent regular expression. In this regular expression, it is ‘\1’, which is equivalent to the first group. Of course, there are cases where we do not want certain groups to be captured. If we want certain groups not to be captured, we can add ‘? : ‘. For example.

letreg = /(\d{5})-(? :\d{3})-(\d{1})/g;Copy the code

The second group of the above re will not be captured, and the ‘\2’ reference group in the subsequent re represents what was captured in the group (\d{1}).

Greedy mode and non-greedy mode

Quantifiers in a re default to greedy mode, which means they match as many characters as possible. For example,

let str = 'ababababababab';
letReg/(ab) = {3, 5} / g; str.replace(reg,'M');  // Mabab
Copy the code

The default greedy pattern matches five ab’s in cases where it can match three to five AB’s. Add? To the quantifier. You can put the re into non-greedy mode. Same thing.

let str = 'ababababababab';
letReg/(ab) = {3, 5}? /g; str.replace(reg,'M');  // "MMab"
Copy the code

Five, forward and negative search

A forward assertion specifies a pattern that must be matched but not returned in the result. A look-forward pattern is really a look forward pattern. A subexpression that begins with =, followed by the text to match. For example.

let str = 'a1bMa2bVa3bKa4bG';
letreg = /(a\db)(? =K)/g; str.match(reg); / / /"a3b"]
Copy the code

(a\db) we know is a child that matches the rule ‘a number b’, and the child after the child is a forward lookup, which specifies that the position after the child must match ‘K’. The only one that fits this rule is ‘a3b’.