Regular summary

Regular expressions are used to match characters in the string pattern combinations. In JavaScript , the regular expressions are objects. These modes are used  RegExp  the  exec  and  test  methods , and the  String  's  match , Replace , Search  and  split  method. This chapter describes JavaScript regular expressions.

Special characters

The so-called special character, is that some characters have special meaning, as said above  runoo * b  in  * , simply means that the string representation of any meaning. If you want to find the string  *  symbol, then you need to  *  escape, that is a plus in front  \Runo \ * ob  match Runo * ob .

Many meta-characters require special treatment when trying to match them. To match these special characters, you must first make the character " escape " , that is, the backslash character \  in front of them. The following table lists the regular expression special characters

 

 

Special character

description

$

Match the end position of the input string. If the set RegExp object Multiline property, $ also matches '\ n' or '\ R & lt' . To match the $ character itself, use \ $ .

( )

Mark the start and end position of a sub-expression. Sub-expressions can be obtained for later use. To match these characters, use \ ( and \) .

*

Matches the preceding subexpression zero or more times. To match * characters, use \ * .

+

Matches the preceding subexpression one or more times. To match the + character, use \ + .

.

In addition to matching newline \ n any single character outside. To match . Please use \. .

[

It marks the start of expression in parentheses. To match [ , use \ [ .

?

Matches the preceding subexpression zero or one, or specify a non-greedy qualifiers. To match ? Characters, use \? .

\

The next character is marked as or special characters, or literals, or back-reference, or an octal escape. For example, 'n-' matches the character 'n-' . '\ n' match a newline. Sequence '\\' match "\" , and '\ (' the matching "(" .

^

Matching the beginning of the string, unless the expression in brackets, which at this time represents not accept the set of characters. To match the ^ character itself, use \ ^ .

{

It marks the start of qualifiers expression. To match { , use \ { .

|

Indicate a choice between the two. To match | , use \ | .


 

Qualifier

Qualifier is used to specify the regular expression of a given component must appear many times to meet the match. There  *  or  +  or  ?  Or  {n}  or  {n,}  or  {n, m}  total . 6 species.

Regular expressions qualifiers are:

character

description

*

Matches the preceding subexpression zero or more times. For example, ZO * can match the "z" and "Zoo" . * Is equivalent to {0} .

+

Matches the preceding subexpression one or more times. For example, '+ ZO' will match "zo" and "Zoo" , but does not match the "Z" . + Is equivalent to {1} .

?

Matches the preceding subexpression zero or one. For example, "do (es)?" Matches "do" , "does" in the "does" , "Doxy" in "do" . ? Is equivalent to {0,1} .

{n}

n is a nonnegative integer. Matching the determined n times. For example, '2 O {}' does not match the "Bob" the 'O' , but can match the "food" in the two O .

{n,}

n is a nonnegative integer. Matching at least n times. For example, '2 O {,}' does not match the "Bob" the 'O' , but it can match "foooood" all in O . 'o {1,}' is equivalent to '+ O' . 'o {0,}' is equivalent to 'O *' .

{n,m}

m and n are non-negative integers, where n <= m . Match at least n times and match up to m times. For example, "O {l, 3}" will match "fooooood" in the first three O . 'o {0,1}' is equivalent to 'O?' . Please note that no spaces between the comma and the two numbers.

 

const r = new RegExp(`\\$\\{${key}\\}`, 'gm');

method

description

exec

Performing a lookup in a matching string RegExp method, which returns an array (not match returns to null ).

test

A test for a match in the string RegExp , which returns true or to false .

match

Performing a lookup in a matching string String method, which returns an array or returned when no match to null .

search

A matching test in the string String method, it returns to the location index matching, or failure to return -1 .

replace

Performing a lookup in a matching string String method, and using the replacement to replace the matched substring string.

split

Using a regular expression or a character string separated by a fixed string, and stores the substring to partition the array String method.

 

Guess you like

Origin blog.csdn.net/qq_41831345/article/details/90262201