JavaScript lastIndex Property
JavaScript RegExp Object
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.
Syntax
Browser Support

The lastIndex property is supported in all major browsers.
Example
Example
Do a global search for "ain" in a string, and output the index after a match is found:
<script type="text/javascript">
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 />");
}
</script> |
Try it yourself »
|
JavaScript RegExp Object
Start Creating a stunning, Flash website. It's easy and free!
Wix.com offers you a simple, powerful, drag & drop editing platform to create stunning Flash websites, layouts, and more.
With added e-commerce features such as search engine visibility and professional tools, Wix is the ultimate solution for creating a spectacular site.
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 4500 certificates already issued!
|
The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|