How to use JavaScript lastIndex Property

This article describe about JavaScript lastIndex Property.
  • 1869

JavaScript lastIndex Property

This property define the index at which to start the next match.
 
 Note: If "g" modifier set then work this property.
 
 For define character position this property return the an integer immediately after the last match found by exec( ) or test( ) methods.
 
 Note: set lastIndex to 0 by exec() and test() if they do not get a match.

Syntax

RegExpObject.lastIndex

Example

<!DOCTYPE html>

<html>

<body>

<script type="text/javascript">

    var str = "The boy climbed on The wall.";

    var pattern = /The/g;

 

    while (pattern.test(str) == true) {

        document.write("'The' found. Index now at: " + pattern.lastIndex);

        document.write("<br />");

    }

</script>

</body>

</html>

 

Output

pic1.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.