X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript lastIndex Property

RegExp Object Reference JavaScript RegExp Object

Example

Do a global search for "ain" in a string, and output the index after a match is found:

var str = "The rain in Spain stays mainly in the plain";
var patt1 = /ain/g;

while (patt1.test(str)==true)
  {
  document.write("'ain' found. Index now at: "+patt1.lastIndex);
  document.write("<br>");
  }

The output of the code above will be:

'ain' found. Index now at: 8
'ain' found. Index now at: 17
'ain' found. Index now at: 28
'ain' found. Index now at: 43

Try it yourself »


Definition and Usage

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

Note: This property only works if the "g" modifier is set.

This property returns an integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods.

Note: exec( ) and test( ) reset lastIndex to 0 if they do not get a match.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The lastIndex property is supported in all major browsers.


Syntax

RegExpObject.lastIndex

Return Value

Type Description
Number An integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods

Technical Details

JavaScript Version: 1.2


RegExp Object Reference JavaScript RegExp Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook