lastIndex

A read/write integer property that specifies the index at which to start the next match.

Property of

RegEx

Description

lastIndex is a property of an individual regular expression object.

This property is set only if the regular expression used the "g" flag to indicate a global search. The following rules apply:

For example, consider the following sequence of statements:

Table 232. lastIndex Statements

StatementDescription
re = /(hi)?/gMatches the empty string.
re("hi")Returns ["hi", "hi"] with lastIndex equal to 2.
re("hi")Returns [""], 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.