Name
RegExp.lastIndex — the starting position of the next match
Availability
JavaScript 1.2; JScript 5.5; ECMAScript v3
Synopsis
regexp.lastIndexDescription
lastIndex is a read/write property of RegExp
objects. For regular expressions with the g
attribute set, it contains an integer that specifies the character
position immediately following the last match found by the
RegExp.exec( ) and RegExp.test( ) methods. These methods use this property as the starting
point for the next search they conduct. This allows you to call those
methods repeatedly, to loop through all matches in a string. Note
that lastIndex is not used by RegExp objects that
do not have the g attribute and do not represent
global patterns.
This property is read/write, so you can set it at any time to specify
where in the target string the next search should begin.
exec( ) and test( )
automatically reset lastIndex to 0 when they fail
to find a match (or another match). If you begin to search a new
string after a successful match of some other string, you have to
explicitly set this property to 0.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access