1. Constructor method
var reg = new RegExp ( 'd' , 'gi' ) ;
2. Literal method
var reg = / d / gi ;
There are three types of modifiers: i, g, m
can appear at the same time, in no order (i.e. gi
is the same as ig
), please refer to the instructions below
modifier | illustrate |
---|---|
i | Ignore case matching |
g | Global matching, that is, matching one match and then continuing to match until the end |
m | Multi-line matching, that is, matching does not stop after encountering a newline until the end |
symbol | effect |
---|---|
As a change of meaning, that is, the characters after "" are usually not interpreted according to the original meaning. For example, /b/ matches the character "b". When /b/ is added in front of b, the meaning of the change is to match a word. boundary. Or the restoration of regular expression function characters, such as " " matches the metacharacter before it 0 or more times, /a / will match a, aa, aaa. After adding "", /a*/ will only match "a *" | |
^ | Matches an input or the beginning of a line, /^a/ matches "an A", but does not match "An a" |
$ | Matches an input or the end of a line, /a$/ matches "An a", but does not match "an A" |
. | Matches any single character, except newlines and terminators |
* | Matches the preceding metacharacter 0 or more times, /ba*/ will match b,ba,baa,baaa |
+ | Matches the preceding metacharacter 1 or more times, /ba+/ will match ba, baa, baaa |
? | Matches the previous metacharacter 0 or 1 times, /ba?/ will match b,ba |
(x) | Match x and save x in variables named $1...$9 |
x | y |
{n} | Match exactly n times |
{n,} | Match n or more times |
{n,m} | Match nm times |
[xyz] | Character set (character set), matches any character (or metacharacter) in this set |
[^xyz] | Does not match any character in this set |
[az] | Any letter [] can represent any one |
[^az] | ^ in non-letters [] represents except |
[b] | Matches a backspace character |
b | Match a word boundary |
B | Match a non-boundary word |
cX | Here, X is a control character, /cM/ matches Ctrl-M |
d | Matches an alphanumeric character, /d/ = /[0-9]/ |
D | Matches a non-alphanumeric character, /D/ = /[^0-9]/ |
n | Matches a newline character |
r | Matches a carriage return |
f | Match form feed |
s | Matches a whitespace character, including n, r, f, t, v, etc. |
S | Matches a non-whitespace character, equal to /[^nfrtv]/ |
t | match a tab character |
v | Matches a double tab character |
w | Matches a character that can form a word (alphanumeric, this is my free translation, including numbers), including underscores, such as [w] matching the 5 in "$5.98", which is equal to [a-zA-Z0-9] |
W | Matches a character that cannot form a word, such as [W] matching the $ in "$5.98", which is equal to [^a-zA-Z0-9] |
Matches NUL characters |
Zero-width assertions are used to find things before or after some content (but not including these content), that is, they are used like b,^,$
to specify a position that should meet certain conditions ( i.e. assertions), so they are also called zero-width assertions
Antecedent assertion | Also called zero-width positive lookahead assertion (?=表达式) which means matching the position before the expression | For example, [az]*(?=ing) can match cook and sing in cooking singing. |
Note: The execution steps of the lookahead assertion are as follows: first find the first ing from the right end of the string to be matched (that is, the expression in the lookahead assertion) and then match the previous expression. If it cannot be matched, continue Find the second ing and then match the string before the second ing. If it matches, then match
Posterior assertion | Also called zero-width post-lookback assertion (?<=表达式) which means matching the position after the expression. | For example (?<=abc).* can match defg in abcdefg |
Note: The late assertion is exactly the opposite of the look-ahead assertion. Its execution steps are as follows: first find the first abc from the leftmost end of the string to be matched (that is, the expression in the look-ahead assertion) and then match the following Expression, if it cannot be matched, continue to search for the second abc and then match the string after the second abc. If it can be matched, it will be matched. For example, (?<=abc).*
can match defgabc in abcdefgabc instead of abcdefg
Negative zero-width assertion | (?!表达式) also matches a zero-width position, but the "assertion" at this position takes the inverse value of the expression. For example, (?!表达式) represents the position before the expression. If the expression does not hold, match this position. ; If the expression is true, it does not match: Similarly, there are two types of negative zero-width assertions: "first" and "last". The negative zero-width assertion is (?<!表达式) |
(?<!表达式)
(?!表达式)
Use parentheses to wrap the matching string, and then use the $1 expression in the replacement value to get the currently matching value. This can be more complicated. For example, if there are multiple parentheses, you can use $1, $2, $3 to get the corresponding value. After replacing all, this is the data we finally want.
For example, the following is a regular expression in the search box, which can match all class
attributes in the HTML structure
className = "([^0-9]+)"
Then we can replace
className = { { $1 } }
Then it will
className = "xxx" //转化为
className = { { xxx } }
1. Methods related to RegExp object
method name | Usage scenarios | return value | Example |
---|---|---|---|
test | Determine whether it matches | true or false | /d/.test('eno yao 2019') |
test | Returns matching results, similar to match | array or null | /d/.exec('eno yao 2019') |
2. String object related methods
method name | Usage scenarios | return value | Example |
---|---|---|---|
match | Returns the matching result. Under non-global conditions, it is consistent with the result returned by exec, and has information pointing to the matching string. Under global conditions, all matching results are returned at once. | array or null | 'eno yao 2019'.match(/d/) |
replace | Replace a string with another string or replace a regular matching string with another substring | array or null | 'eno yao 2019'.replace(/d/, '2019') |
search | Find the position of the first matching substring and return the index value, otherwise return -1 | index | 'eno yao 2019'.search(/d/, '2019') |
split | Split the array according to the agreed string or string, accept a string or regular | index | 'eno yao 2019'.search(/d/, '2019') |