JavaScript lastIndex Property
Definition and Usage
The lastIndex property is used to specify the starting position for the next match.
Note: This property is only effective when the global flag g is set.
The result of the last match is found by the methods RegExp.exec() and RegExp.test(), which both use the position pointed to by the lastIndex property as the starting point for the next search. This allows for iterating through all matches in a string by repeatedly calling these methods.
Note: This property is read/write. It can be set whenever the next search in the target string begins. When methods exec() or test() can no longer find matching text, they automatically reset the lastIndex property to 0.
Syntax
Browser Support
All major browsers support the lastIndex property.
Example
Example
Globally search for the substring "ain" in a string and output the lastIndex property after the match: