Name
exec() — NN 4 IE J3 ECMA n/a
Synopsis
exec(string)Performs a search through the string passed as a parameter for the current regular expression pattern. A typical sequence follows the format:
var myRE = /somePattern/
var resultArray = myRE.exec("someString")Properties of both the static RegExp and regular
expression (myRE in the example) objects are
updated with information about the results of the search. In
addition, the exec() method returns an array of
data, much of it similar to RegExp object
properties. The returned array includes the following properties:
-
index Zero-based index of starting character in the string that matches the pattern
-
input The original string being searched
-
[0] String of the characters matching the pattern
-
[1]...[n] Strings of the results of the parenthesized component matches
You can stow away the results of the exec() method
in a variable, whereas the RegExp property values
change with the next regular expression operation. If the regular
expression is set for global searching, a subsequent call to
myRE.exec("someString") continues the search from
the position of the previous match.
If no match is found for a given call to exec(),
it returns null.
Returned Value
An array of match information if successful; null
if there is no match.
Parameters
-
string The string to be searched.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access