Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. A regular expression is a powerful weapon used to match strings. It is designed to define a rule for a string in a descriptive language, and any string that conforms to the rule, we think it "matches".
\d
Can match a number
‘00\d‘
Can match
‘007‘
,
‘\d\d\d‘
Can match
‘010‘
\w
Can match a letter or number
‘\w\w‘
Can match
‘js‘
\s
Can match a space (also including tab and other whitespace characters)
\DWS
Matches the matching relationship with the lowercase letter.
\数字n
Represents a reference to the text that is captured by grouping N, which can be matched again
/(\d+)(0*)\1/
Can match ' 22300223 '
.
can match any character
‘js.‘
Can match
‘jsp‘
、
‘jss‘
、
‘js!‘
*
Represents any character (including 0)
+
Represents at least one character
?
Represents 0 or 1 characters
{n}
Represents n characters
{n,m}
Represents a n-m character
A|B
Can match A or b
(J|j)ava(S|s)cript
Can match
‘JavaScript‘
、
‘Javascript‘
、
‘javaScript‘
Or
‘javascript‘
^
Represents the beginning of a row
^\d
Indicates that a number must begin
$
Indicates the end of a row
\d$
Indicates the need to end with a number
[]
Representation Range
[]
In the
^
means to exclude a character
[^#?]
Except for that? and # all the characters
[a-zA-Z\_\$][0-9a-zA-Z\_\$]*
Can be matched by a letter or underscore, a $, followed by a string consisting of a number, letter, or underscore, which is the variable name allowed by JavaScript
[a-zA-Z\_\$][0-9a-zA-Z\_\$]{0, 19}
More precisely limit the length of a variable to 1-20 characters (1 characters + 19 characters before) using regular expressions to slice a string more flexibly than a fixed character
' A, b;; C d '. Split (/[\s\,\;] +/); [' A ', ' B ', ' C ', ' d ']
In addition to simply judging whether a match is matched, the regular expression also has the power to extract substrings. Use
()
Represents the group to extract if a group is defined in a regular expression, you can
RegExp
Object with the
exec()
method to extract the substring from the string.
exec()
After the match succeeds, returns a
Array
, the first element is the entire string to which the regular expression matches, followed by a string that represents the successful substring.
exec()
return if match fails
null
。
^(\d{3})-(\d{3,8})$
Two groups are defined separately, and the area code and local numbers can be extracted directly from the matching string:
var re =/^ (\d{3})-(\d{3,8}) $/;re.exec (' 010-12345 '); [' 010-12345 ', ' 010 ', ' 12345 ']re.exec (' 010 12345 '); Null
of the RegExp object
test()
method is used to test whether a given string conforms to a condition.
var re =/^\d{3}\-\d{3,8}$/; re.test (// true// false
It should be noted that the regular match is the default
Greedy Match, which is to match as many characters as possible.
var re =/^ (\d+) (0*) $/; re.exec (// [' 102300 ', ' 102300 ', ']
Because
\d+
Using greedy match, directly to the back of the
0
All matched, the result
0*
Only empty strings are matched.
Add one to ?
make a \d+
non-greedy match:
var re =/^ (\d+?) (0*) $/; re.exec (// [' 102300 ', ' 1023 ', ' xx ' )
?: Do not want to be captured when used, can improve the program execution speed such as ([a-z][0-9]) +, () inside the content is captured, if written (?: [a-z][0-9]) +, the overall match is the same, is not capturing () content. Special signs
g
Flag that represents the global match
i
Flag, indicating ignoring case
m
Flag, indicating that multiple line matching is performed
"RegExp" javascript normal expression judgment matching rules and common methods