Name
String.match() — find one or more regular-expression matches
Synopsis
string
.
match
(
regexp
)
Arguments
regexp
A RegExp object that specifies the pattern to be matched. If this argument is not a RegExp, it is first converted to one by passing it to the
RegExp()
constructor.
Returns
An array containing the results of the match. The contents
of the array depend on whether regexp
has the global “g” attribute set. Details on this return value are
given in the Description.
Description
match()
searches
string
for one or more matches of
regexp
. The behavior of this method
depends significantly on whether regexp
has the “g” attribute or not (see Chapter 10 for
full details on regular expressions).
If regexp
does not have the “g”
attribute, match()
searches
string
for a single match. If no match is
found, match()
returns null
. Otherwise, it returns an array
containing information about the match that it found. Element 0 of
the array contains the matched text. The remaining elements contain
the text that matches any parenthesized subexpressions within the
regular expression. In addition to these normal array elements, the
returned array also has two object properties. The index
property of the array specifies the
character position within string
of the
start of the matched text. Also, the input
property of the returned array is a
reference to string
itself.
If regexp
has the “g” flag,
match()
does a global search,
searching string
for all matching
substrings. It returns null
if no match is found, ...
Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.