JavaScript tutorial:
lastIndex property (Regular Expression)

 

Applies To: Regular Expression Object

The lastIndex property specifies the index at which to start the next match.

Syntax

rgexp.lastIndex [= index]

The lastIndex property syntax has these parts:

Part

Description

rgexp

Required. A Regular Expression object. Can be a variable name or a literal.

index

The index from which to begin the next search.

Remarks

The lastIndex property is modified by the exec method, and the match, replace, and split methods of the String object.

The following rules apply to values of lastIndex:

If lastIndex is greater than the length of the string, the test and exec methods fail, and lastIndex is set to zero.

If lastIndex is equal to the length of the string, the regular expression matches if the pattern matches the empty string. Otherwise, the match fails and lastIndex is reset to zero.

Otherwise, lastIndex is set to the next position following the most recent match.

See Also: match Method, Regular Expression Object Methods, Regular Expression Object Properties, Regular Expression Syntax, replace Method, split Method, String Object