We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS



The exec() method executes a search for a match in a specified string. Returns a result array, or null.

If you are executing a match simply to find true or false, use the RegExp.test() method or the String.search() method.




The string against which to match the regular expression.

Return value

If the match succeeds, the exec method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

If the match fails, the exec method returns null.


Consider the following example:

// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case
var re = /d(b+)(d)/ig;
var result = re.exec("cdbBdbsbz");

The following table shows the results for this script:

Object Property/Index Description Example
result [0] The last matched characters dbBd
[1], ...[ n ] The parenthesized substring matches, if any. The number of possible parenthesized substrings is unlimited. [1] = bB
[2] = d
index The 0-based index of the match in the string. 1
input The original string. cdbBdbsbz
re lastIndex The index at which to start the next match. 5
ignoreCase Indicates if the "i" flag was used to ignore case. true
global Indicates if the "g" flag was used for a global match. true
multiline Indicates if the "m" flag was used to search in strings across multiple line. false
source The text of the pattern. d(b+)(d)


Example: Finding successive matches

If your regular expression uses the "g" flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property). For example, assume you have this script:

var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) !== null)
  var msg = "Found " + myArray[0] + ".  ";
  msg += "Next match starts at " + myRe.lastIndex;

This script displays the following text:

Found abb. Next match starts at 3
Found ab. Next match starts at 9

Note: Do not place the regular expression literal (or RegExp constructor) within the while condition or it will create an infinite loop if there is a match due to the lastIndex property being reset upon each iteration.

Example: Using exec() with RegExp literals

You can also use exec() without creating a RegExp object:

var matches = /(hello \S+)/.exec('This is a hello world!');

This will display an alert containing 'hello world!';


Specification Status Comment
ECMAScript 3rd Edition. Implemented in JavaScript 1.2 Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'RegExp.exec' in that specification.
ECMAScript 6 (ECMA-262)
The definition of 'RegExp.exec' in that specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Document Tags and Contributors

Contributors to this page: Sevenspade
Last updated by: Sevenspade,