exec()
method executes a search for a match in a specified string. Returns a result array, or
null
.
JavaScript
RegExp
对象是
stateful
when they have the
global
or
sticky
flags set (e.g.
/foo/g
or
/foo/y
). They store a
lastIndex
from the previous match. Using this internally,
exec()
can be used to iterate over multiple matches in a string of text (with capture groups), as opposed to getting just the matching strings with
String.prototype.match()
.
A newer function has been proposed to simplify matching multiple parts of a string (with capture groups):
String.prototype.matchAll()
.
If you are executing a match simply to find
true
or
false
,使用
RegExp.prototype.test()
method or
String.prototype.search()
代替。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
regexObj.exec(str)
str
The string against which to match the regular expression.
If the match succeeds, the
exec()
method returns an array (with extra properties
index
and
input
; see below) and updates the
lastIndex
property of the regular expression object. The returned array has the matched text as the first item, and then one item for each parenthetical capture group of the matched text.
If the match fails, the
exec()
method returns
null
, and sets
lastIndex
to
0
.
Consider the following example:
// Match "quick brown" followed by "jumps", ignoring characters in between
// Remember "brown" and "jumps"
// Ignore case
let re = /quick\s(brown).+?(jumps)/ig;
let result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');
The following table shows the results for this script:
| Object | Property/Index | 描述 | 范例 |
|---|---|---|---|
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.
若
|
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:
let myRe = /ab*/g;
let str = 'abbcdefabh';
let myArray;
while ((myArray = myRe.exec(str)) !== null) {
let 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
警告:
Do
not
place the regular expression literal (or
RegExp
constructor) within the
while
condition!
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 it will also cause an infinite loop.
exec()
with
RegExp
literals
You can also use
exec()
without creating a
RegExp
object explicitly:
let matches = /(hello \S+)/.exec('This is a hello world!');
console.log(matches[1]);
This will log a message containing
'hello world!'
.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.exec' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
exec
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 5 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
RegExp
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()