lastIndex  RegExp  instance property JS Home  <<  JS Reference  <<  RegExp  <<  lastIndex

Description

Specifies the index to start the next match from.

  • lastIndex only works when the g flag has been set.

Syntax

Signature Description
aRegExp.lastIndexReturns a reference to the function that created the prototype.
  • The exec() and test() methods reset the lastIndex property to 0 if no match is found

Parameters

None.

Examples

The code below displays the lastIndex property of a RegExp instances.


var aRegExp = /and/g;
var aString = new String 'one and two and three and four';
var flagValues = new Array();
var i = 0;

while (aRegExp.test(aString)==true) {
  flagValues[i] = 'the word and found. Match will continue at index: ' 
	+ aRegExp.lastIndex + '\n'; 
  i++;
}

alert(flagValues);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 9 - Regular Expressions