Regular expression

Regular expression creation

Expression metacharacter

character meaning
{n} Matches the preceding character or expression, specifying the number of matches to be n
{n,} Matches the preceding character or expression, specifying the number of matches to be at least n
{n,m} Matches the preceding character or expression, specifying the number of matches at least n to a maximum of m times
[xyz] A collection of characters that matches any character contained in it. For example, [abcd] represents any character in abcd. You can also use – for range constraints, for example[a-z], [0-9], [a-z], [a - z0-9], [a - z0-9 _], [\ u4e00 - \ u9fa5]Etc.
[^xyz] Matches any other character that does not contain any of the characters. For example,[^abcd]Stands for any character except abcd
\d If any of the digits in 0-9 are matched, it is equivalent to [0-9].
\D Matching a non-Arabic number is equivalent to[^ 0-9].
\f Matches a feed character (U+000C).
\n Matches a newline character (U+000A).
\r Matches a carriage return (U+000D).
\s Matches a whitespace character, including a space, TAB, page feed, and line feed. Is equivalent to[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
\S Matches a non-whitespace character. Is equivalent to[^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
\t Matches a horizontal TAB character (U+0009).
\v Matches a vertical TAB character (U+000B).
\w Matches a single word character (letter, number, or underscore). Is equivalent to[A-Za-z0-9_].
\W Matches a non-single-word character. Is equivalent to[^A-Za-z0-9_].

Regular expression method

character meaning
exec Finds a string in a string that matches the rule, and returns an array (null if no match is found). One match at a time will return lastIndex to indicate the starting index value of the next match. When we execute exec again, the search will automatically continue from here until null
test Tests whether the string matches the rule. It returns true or false

Advanced search flag

character meaning
g Global search
i Case insensitive search

Greedy versus non-greedy

Greedy metacharacters include: {n,}, {n,m},? , *, +

Greedy mode features: matches as many characters as possible

Non-greedy mode features: Matches as few characters as possible

Appends the greed mode metacharacter with? Change it to non-greedy mode: {n,}? , {n, m}? And???? , *? , +,?

// Greedy mode
let str = 'd131adafdae';
let reg = /d\w+/;       //\w+ will match as many characters as possible after d
console.log(reg.exec(str));
let reg2 = /d\w+d/;     // Although \w contains d, \w will yield the last matched d in order for the whole expression to match successfully
console.log(reg2.exec(str));

// Non-greedy mode
let reg3 = /d\w+? /;     //\w+? Matches only 1 character after d
console.log(reg3.exec(str));
let reg4 = /d\w+? d/;     In order for the whole expression to match successfully, you have to match a few more characters to make the following d match
console.log(reg4.exec(str));
Copy the code