What is a regular expression?

A regular expression is a string matching method consisting of common characters and metacharacters. It’s basically a string template of some kind, matching a string.

The main grammar

Regular expressions are written in // and in //. The ^ symbol indicates the beginning of the preceding character and the $symbol indicates the end of the preceding character.

/ ^ [0-9]+$/gi
Copy the code

Character rule

Common characters:

A regular character is any character that can be typed on the keyboard and written in a regular expression to match:

/abcdefg123,.; /Copy the code

Metacharacters:

Metacharacters are complex, including ordinary metacharacters, special metacharacters, qualifiers, modifiers, and so on

  • Ordinary metacharacter

character describe
\f Matches a feed character. Is equivalent to\x0c\cL.
\n Matches a newline character. Is equivalent to\x0a\cJ.
\r Matches a carriage return. Is equivalent to\x0d\cM.
\t Matches a TAB character. Is equivalent to \x09\cI.
\v Matches a vertical TAB character. Is equivalent to\x0b\cK.
\b Matches a word boundary. Between words and Spaces, for example,er\bYou can match the ER in over, but not the ER in serve
\B Matches a non-word boundary
\s Matches any whitespace character, including Spaces, tabs, page feeds, and so on. Is equivalent to[ \f\n\r\t\v]. Note that Unicode regular expressions match full-corner Spaces.
\S Matches any non-whitespace character. Is equivalent to [^ \f\n\r\t\v].
\d Matches numeric characters. Is equivalent to[0-9].
\D Matches non-numeric characters. Is equivalent to[^ 0-9].
\w Matches letters, digits, and underscores. Is equivalent to[A-Za-z0-9_].
\W The value cannot contain letters, digits, or underscores. Is equivalent to[^A-Za-z0-9_].
  • Special metacharacter

character describe
^ Matches the beginning of the input string, when present in [] to indicate that the rule in brackets does not match.
$ Matches the end of the input string.
( ) Start and end positions of a subexpression. Subexpression can be retrieved for later use (by [$n]).
[] Brackets expression that matches any character in brackets.
. Matches any non-newline character.
\ Marks the next character as a special character, or a literal character, or a backreference, or an octal escape.
| Said one option character, example: / (a | b) /, matching a or b
  • qualifiers

character describe
* Matches the preceding subexpression zero or more times.
+ Matches the previous subexpression one or more times.
? Matches the preceding subexpression zero or once.
{n} N is a non-negative integer. Match certain n times.
{n,} N is a non-negative integer that matches at least n times.
{m,n} Both m and n are non-negative integers, where n <= m. At least n times and at most m times are matched.
  • The modifier

character describe
g Global matching.
i Case insensitive.
m Multi-line matching.
s Special metacharacter.Contains a newline character.
y Sticky match, it starts at lastIndex.

Match the API

Regular expression API

  • The exec:reg.exec(str)
    • Returns an array with [0] entries for all matched strings, [0],… [n] is captured separately in parentheses, with additional attributes index being the zero-based index of the matched original string, input being the original string;
    • Return: NULL;
let reg = /quick\s(brown).+? (jumps)/ig;
let result = reg.exec('The Quick Brown Fox Jumps Over The Lazy Dog');
// [0: "Quick Brown Fox Jumps",
// 1: "Brown",
// 2: "Jumps",
// groups: undefined,
// index: 4,
// input: "The Quick Brown Fox Jumps Over The Lazy Dog",
// length:3]
let reject = reg.exec('aaaaaaa');
// null
Copy the code
  • The test:reg.test(str)
    • Returns: true;
    • Return: false;
let reg = /^\/[a-zA-Z0-9]+$/;
let result = reg.test('/abc0');  // true
let reject = reg.test('//--a8');  //false
Copy the code

String API

  • Search:str.search(reg)
    • If the match is successful, the index subscript of the first match is returned.
    • If the match fails, -1 is returned.
let str = "hello world!";
let reg = /[^\w]/g;
let result = str.search(reg); / / 5
Copy the code
  • Match:str.match(reg)
    • Returns an array of matching results if the match is global; If there is no global match, an array with special attributes is returned. The [0] item is the match result, the Groups attribute is the capture array name undefined, the index attribute is the starting subscript of the match result, and the input attribute is the original string.
    • Return: NULL;

If the regular expression does not contain the G flag, str.match() will return the same result as regexp.exec ().

let regG = /[A-Z]/g;
let reg = /[A-Z]/;
let str = "Hello World!";
str.match(regG); //["H","W"]
str.match(reg); 
/*[ 0:"H", groups: undefined, index: 0, input: "Hello World!" ] * /
Copy the code
  • Replace:Str.replace (reg, 'replace string ')
// Example: Change the middle four digits of the mobile phone number to "****"
let reg = /^1(\d{2})\d{4}(\d{4})$/g;
let tell = "15807928848";
let result = tell.replace(reg, "1 $1 $2 * * * *"); * * * * 8848 / / 158
// $1 = reg (\d{2}); $2 = reg (\d{4}
Copy the code

$1–$9 comes with RegExp, whenever a match occurs. Represents the grouping, that is, the contents captured by the little re inside the parentheses. / ^ (\ d {4}) [-] / (\ d {1, 2}) [-] / (\ d {1, 2}) $/ such as the simple matching (date) (month) (year) of a regular, we can through the $1, $2, $3 is available for processing.

  • The split:str.split(reg)

senior

Statement a vocabulary

  • Forward declaration (? = Matching condition) : Specifies that the characters following the matching pattern must be matched, but does not return these characters (that isMatch the patternThe characters immediately following must be metMatching conditionsTo be matched successfully)
'Match pattern'(? ='Match condition')
let reg = /(\d)+(? =d)/g;
let str = 'd123abc456def';
reg.exec(str);// ['456','6',index: 7, input: 'd123abc456def', groups: undefined]
Copy the code
  • Reverse declaration (? ! Matching conditions) : In contrast to a forward statement match, specify that none of the following characters need be matched (that isMatch the patternThe characters immediately following must not be metMatching conditionsTo be matched successfully)
'Match pattern'(? !'Match condition')
let reg = /(\d){3}(? ! d)/g;
let str = 'd123abc456def';
reg.exec(str);// ['123','3',index: 1, input: 'd123abc456def', groups: undefined]
Copy the code

reference

  • Backreference:\ Numeric number(Special escape)

The number specifies the order of the subexpression in the matching pattern. For example, “\1” refers to the first subexpression, and “\2” refers to the second subexpression.

let str = "a3 3 a 3a";
let reg = /(\w)(\d)(\s)\2\3\1\3\2\1/;
reg.test(str); // true
Copy the code
  • Do not quote:? :

In longer regular expressions, backreferences slow down matching, and you can disable them if grouping is just for convenience.

let str = "Harry Trump ; Fred Barney; Hello ";
let reg = /\s*(? :; |$)\s*/;
str.split(reg); // ["Harry Trump", "Fred Barney", ""]
Copy the code