Getting StartedCharacter Matching
- \d Match a number
- \w matches an array or a letter
- . Match any character
- \s Match a space
Quantity Matching
- * denotes any character (including 0)
- + indicates a minimum of one character
?
Represents 0 or 1 characters
- {n} represents n characters
- {N,m} represents n-m characters ({} Yes, not-)
PS: Special characters, in regular expressions, to be ‘\‘
escaped
Practice:
- \d{3}\s+\d{3,8} Answer: Match 3 digits + at least one space +3-8 number
- Like "010-12345" this number answer: \d{3}\-\d{5}
Advanced
To make a more precise match, you can use the []
representation range. Like what:
[0-9a-zA-Z\_]
Can match a number, letter, or underscore;
[0-9a-zA-Z\_]+
Can match a string of at least one number, letter, or underscore, for example, and ‘a100‘
‘0_Z‘
‘js2015‘
so on;
[a-zA-Z\_\$][0-9a-zA-Z\_\$]*
You can match a string consisting of a number, letter, or underscore, or $, which is the name of the variable allowed by JavaScript, by a letter or underscore, or $.
[a-zA-Z\_\$][0-9a-zA-Z\_\$]{0, 19}
More precisely limit the length of a variable to 1-20 characters (1 characters before + 19 characters later).
A|B
Can match A or B, so [J|j]ava[S|s]cript
you can match ‘JavaScript‘
, ‘Javascript‘
, ‘javaScript‘
or ‘javascript‘
.
^
Represents the beginning of a row, ^\d
indicating that a number must begin.
$
Represents the end of a line, indicating that it \d$
must end with a number. You may have noticed,
js
can also be matched ‘jsp‘
, but plus ^js$
it becomes an entire line match, it can only match ‘js‘
.
RegExp
With the knowledge of readiness, we can use regular expressions in JavaScript.
JavaScript has two ways of creating a regular expression:
The first way is by /正则表达式/
writing it directly, and the second way is by new RegExp(‘正则表达式‘)
creating a RegExp object.
The two formulations are the same:
var Re1 =/abc\-001/;var Re2 = new RegExp (' abc\\-001 '); Re1; /abc\-001/re2; /abc\-001/
Note that if you use the second notation because of the escape problem of the string, the two of the string \\
is actually one \
.
Let's look at how to tell if a regular expression matches:
var re =/^\d{3}\-\d{3,8}$/;re.test (' 010-12345 '); Truere.test (' 010-1234x '); Falsere.test (' 010 12345 '); False
The method of the RegExp object test()
is used to test whether a given string conforms to a condition.
slicing a string
Using regular expressions to slice a string is more flexible than a fixed character, see the normal segmentation code:
' A B c '. Split (');//[' A ', ' B ', ' ', ', ' C ')
Well, you can't recognize contiguous spaces, try using regular expressions:
' A B c '. Split (/\s+/);//[' A ', ' B ', ' C ']
No matter how many spaces can be divided normally. Add to try ,
:
' B, c d '. Split (/[\s\,]+/);//[' A ', ' B ', ' C ', ' d ']
Try again ;
:
' A, b;; C d '. Split (/[\s\,\;] +/); [' A ', ' B ', ' C ', ' d ']
If the user enters a set of tags, next time remember to use regular expressions to convert the nonstandard input into the correct array.
Grouping
In addition to simply judging whether a match is matched, the regular expression also has the power to extract substrings. The ()
Grouping (group) to be extracted is represented by the. Like what:
^(\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
If a group is defined in a regular expression, you can extract the substring from the RegExp
object using a exec()
method.
exec()
The method returns one after the match succeeds, the Array
first element is always the original string itself, and the subsequent string represents the substring that matches the success.
exec()
Method is returned when a match fails null
.
Extracting substrings is useful. Look at a more vicious example:
var re =/^ (0[0-9]|1[0-9]|2[0-3]|[ 0-9]) \:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]| [0-9]) \:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]| [0-9]) $/; Re.exec (' 19:05:30 '); [' 19:05:30 ', ' 19 ', ' 05 ', ' 30 ']
This regular expression can directly identify the legal time. However, there are times when it is not possible to fully validate with regular expressions, such as identifying dates:
var re =/^ (0[1-9]|1[0-2]|[ 0-9])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[ 0-9]) $/;
For ‘2-30‘
, ‘4-31‘
such illegal date, with regular or can not be recognized, or write out to be very difficult, then need to program with identification.
Greedy Match
In particular, a regular match is a greedy match by default, which is to match as many characters as possible. For example, match the following numbers 0
:
var re =/^ (\d+) (0*) $/;re.exec (' 102300 '); [' 102300 ', ' 102300 ', ']
Because \d+
of the greedy match, directly the back of 0
all matching, the result 0*
can only match the empty string.
\d+
a non-greedy match (that is, as few matches as possible) must be used in order to match the latter 0
and add a ?
\d+
non-greedy match to it:
var re =/^ (\d+?) (0*) $/;re.exec (' 102300 '); [' 102300 ', ' 1023 ', ' 00 ']
Global Search
JavaScript regular expressions also have several special flags, most commonly used g
to represent global matches:
var r1 =/test/g;//equivalent to: var r2 = new RegExp (' Test ', ' G ');
A global match can execute exec()
the method multiple times to search for a matching string. When we specify a g
flag, each time it is run exec()
, the regular expression itself updates the lastIndex
property, representing the last index to which it was last matched:
var s = ' JavaScript, VBScript, JScript and ECMAScript '; var re=/[a-za-z]+script/g;//uses global match: re.exec (s); [' JavaScript ']re.lastindex; 10re.exec (s); [' VBScript ']re.lastindex; 20re.exec (s); [' JScript ']re.lastindex; 29re.exec (s); [' ECMAScript ']re.lastindex; 44re.exec (s); Null until the end is still not matched to
The global match is similar to a search and therefore cannot be used, so it /^...$/
will only match at most once.
The regular expression can also specify i
flags, which indicate that the case is ignored, and the flag indicates that a m
multiline match is performed.
Practice:
- Please try to write a regular expression that verifies the email address: var re =/^[\w\.] [Email protected]\w+\.\w+$/;
JavaScript Basics-Pattern matching