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.prototype.test()
method or the String.prototype.search()
method.
regexObj.exec(str)
str
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 "quick brown" followed by "jumps", ignoring characters in between // Remember "brown" and "jumps" // Ignore case var re = /quick\s(brown).+?(jumps)/ig; var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');
The following table shows the results for this script:
Object | Property/Index | Description | Example |
---|---|---|---|
result | [0] | The full string of characters matched | "Quick Brown Fox Jumps" |
[1], ...[n] | The parenthesized substring matches, if any. The number of possible parenthesized substrings is unlimited. | result[1] === "Brown" | |
index | The 0-based index of the match in the string. | 4 | |
input | The original string that was matched against. | "The Quick Brown Fox Jumps Over The Lazy Dog" | |
re | lastIndex | The index at which to start the next match. If g is absent, this will always be 0 . | 25 |
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 across multiple lines. | false | |
source | The text of the pattern. | "quick\s(brown).+?(jumps)" |
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). Note that the lastIndex
property will not be reset when searching a different string it will start its search at its existing lastIndex
.
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; console.log(msg); }
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. Also be sure that the global flag ("g
") is set or an infinite loop will occur here also.
exec()
with RegExp
literalsYou can also use exec()
without creating a RegExp
object:
var matches = /(hello \S+)/.exec('This is a hello world!'); console.log(matches[1]);
This will log a message containing 'hello world!'.
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) The definition of 'RegExp.exec' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'RegExp.exec' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'RegExp.exec' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 1 | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
Server | |
---|---|
Node.js | |
Basic support | Yes |
RegExp
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec