Regular expression
We have two declarations of
regexp
new RegExp('
template
', '
flags
')
/
template
/
flags
Example
Flags
i, g, m, u, y, s
i
uncase-sensitive
g
search for all matches
m
multiline mode
u
class
unicode
y
search for specified index
s
search for space
i
g
m
u
y
s
Metacharacters
of
template
\d \w \s \n .
\d
-
digital [0-9]
\w
-
word [a-z0-9_]
\s
-
space
\n
- brake word
.
-
all symbols
\t \f \r \v \0 [\b] \cX \xhh \uhhhh
\u{hhhh} \u{hhhhh} \
\w
\s
\n
.
\W \S \D
\W
-
not a word
\D
-
not a digital
\S
-
not a space
\W
\D
\S
Exceptions
are also metacharacters
^ $ \b \B
^
$
\b
\B
Multiline
^
$
Quantifiers
+ * ? n{}
+
:
one and more
*
:
zero and more
?
:
zero or one
n{}
:
spread
+
*
?
n{}
Example
Lazy and Greedy
quantifiers
Greedy (by default)
+ * ?
Lazy
+? *? ??
Sets and Ranges
[
ab.fr
] [
12_$"
]
[
15
] [
a31-
]
Sets
Example
Ranges
[
1
-
9
]
[
1
-
5
]
[
a
-
z
]
[
a
-
g
]
[
A
-
Z
] [
1
-
9a
-
z
]
[
1
-
5A
-
F
]
Example
We can also use
metacharacters
in
brackets
Exclusive
Sets and Ranges
[^]
Sets
Ranges
Bracket groups
()
Example
Search engine
can memorize data of
bracket groups
.
[
0
]
The first value of the variable's
result
will output full
template pattern
.
Where is this data located?
Unnecessary
Bracket groups
()?
Named group
(?<name>)
Exclusion from memory
(?:)
Backlinks in the template
Example
\n
or
\k<
name
>
Example
Example
Alterations, Lookahead and Retrospective checks
Alteration
|
Lookahead check
X
(?=
Y
)
Negative
Lookahead check
X
(?!
Y
)
Retrospective check
(?<=
Y
)
X
Negative
Retrospective check
(?<!
Y
)
X
Example
Catastrophic backtracking
Example
Example
Example
Start
Finish
2^n -1
We have two way to decide this problem.
The fist way
Reduce the number of combinations
The second way
No refunds
Atomic bracket groups (
don't support
)
Possessive quantifier (
don't support
)
?
Lookahead (
support
)
Example
Example
Example
Methods & Properties
Methods
match()
matchAll()
split()
search()
replace()
Special symbol
$$
- insert $
$&
- inserts all found matches
$'
- insert a part of string before
$`
- insert a part of string after
$n
- inserts the contents of the nth bracket
$<name>
- inserts the contents of the name bracket
Example
The second
parameter
of the
replace
can be a
function
The
parameters
of the
function
match
- a match is found
p1
- content of brackets
p2
- content of brackets
pn
- content of brackets
offset
- position where the match was found
input
- original string
groups
-
object with the contents of named brackets
Example
Example
Example
Example
exec()
test()
startsWith()
endsWith()
includes()
Properties
.flags
- check by
all of flags
.dotAll
- check by
s
flag
.global
- check by
g
flag
.
ignoreCas
e
- check by
i
flag
.multiline
- check by
m
flag
.unicode
- check by
u
flag
.sticky
- check by
y
flag
.source
- check by
string
without
regexp
.lastIndex
- it'
s a
read/write
integer property of regular expression instances that specifies the
index
at which
to start the next match
.
Links
Handbook
Learn.javaScript.ru
MDN
Instanceof me
Flagrant Badassery
Lookahead
Possessive Quantifiers
Atomic Grouping
Made with Slides.com