lastIndex
is a read/write integer property of regular expression instances that specifies the index at which to start the next match.
特性属性在
RegExpInstance.lastIndex
|
|
|---|---|
| 可写 | yes |
| 可枚举 | no |
| 可配置 | no |
This property is set only if the regular expression instance used the
g
flag to indicate a global search, or the
y
flag to indicate a sticky search. The following rules apply:
lastIndex
is greater than the length of the string,
test()
and
exec()
fail, then
lastIndex
is set to 0.
lastIndex
is equal to or less than the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting from
lastIndex
.
lastIndex
is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and
lastIndex
is reset to 0.
lastIndex
is set to the next position following the most recent match.
Consider the following sequence of statements:
var re = /(hi)?/g;
Matches the empty string.
console.log(re.exec('hi'));
console.log(re.lastIndex);
返回
["hi", "hi"]
with
lastIndex
equal to 2.
console.log(re.exec('hi'));
console.log(re.lastIndex);
返回
["", undefined]
, an empty array whose zeroth element is the match string. In this case, the empty string because
lastIndex
was 2 (and still is 2) and
hi
has length 2.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp.lastIndex' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
lastIndex
|
Chrome 1 | Edge 12 | Firefox 1 | IE 5.5 | 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.prototype.ignoreCase
RegExp.prototype.global
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
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()