Name
String.match( ): find one or more regular-expression matches â ECMAScript v3
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 11
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. ...
Get JavaScript: The Definitive Guide, 5th 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.