JavaScript RegExp.prototype.lastIndex Property
The lastIndex property holds the position where a global or sticky regular expression will start its next search.
Description
The lastIndex property of a regular expression with the g or y flag holds the index to start the next match from. exec() and test() update it after each match and reset it to 0 when no match is found. Set it to 0 yourself to restart a search.
It also affects exec() and string methods that use the regex, such as replace() with a global pattern, which resets it to 0 when done. A common bug is reusing a global regex stored in a variable or constant to test several strings: each test starts where the last one ended, so some matches are missed.
The simplest fix is to leave out the g flag when you only need to test for a match, or set lastIndex = 0 before each use. With the sticky y flag, a match must start exactly at lastIndex, which is useful for writing tokenizers.
Syntax
regexp.lastIndexValue
A non-negative integer: the index at which the next match will start.
Example
Browser Support
The following information will show you the current browser support for the RegExp.prototype.lastIndex property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





