RegExp
For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.
Last updated
For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.
Last updated
Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length
, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf()
method, or extracting substrings with the substring()
method.
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec()
and test()
methods of RegExp
, and with the match()
, matchAll()
, replace()
, replaceAll()
, search()
, and split()
methods of String
. This chapter describes JavaScript regular expressions.
Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:
Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.
Or calling the constructor function of the RegExp
object, as follows:
Special characters in regular expressions.
Characters / constructs
Corresponding article
\
, .
, \cX
, \d
, \D
, \f
, \n
, \r
, \s
, \S
, \t
, \v
, \w
, \W
, \0
, \xhh
, \uhhhh
, \uhhhhh
, [\b]
^
, $
, x(?=y)
, x(?!y)
, (?<=y)x
, (?<!y)x
, \b
, \B
(x)
, (?:x)
, (?<Name>x)
, x|y
, [xyz]
, [^xyz]
, \
Number
*
, +
, ?
, x{
n
}
, x{
n
,}
, x{
n
,
m
}
\p{
UnicodeProperty
}
, \P{
UnicodeProperty
}