The lastIndex
is a read/write integer property of regular expression instances that specifies the index at which to start the next match.
Property attributes of regexp.lastIndex
| |
---|---|
Writable | yes |
Enumerable | no |
Configurable | no |
regExpObj.lastIndex
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 the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at 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.lastIndex
Consider the following sequence of statements:
var re = /(hi)?/g;
Matches the empty string.
console.log(re.exec('hi')); console.log(re.lastIndex);
Returns ["hi", "hi"]
with lastIndex
equal to 2.
console.log(re.exec('hi')); console.log(re.lastIndex);
Returns ["", 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.
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. JavaScript 1.5: lastIndex is a property of a RegExp instance, not the RegExp object. |
ECMAScript 5.1 (ECMA-262) The definition of 'RegExp.lastIndex' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'RegExp.lastIndex' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'RegExp.lastIndex' 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.prototype.ignoreCase
RegExp.prototype.global
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
© 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/lastIndex