index Property
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

index Property

Returns the character position where the first successful match begins in a searched string.

{RegExp | reArray}.index


Required. The global RegExp object.
Required. An array returned by the exec method of a Regular Expression object.


The index property is zero-based.

The initial value of the RegExp.index property is –1. Its value is read-only and changes whenever a successful match is made.

Note   The properties of the RegExp object are not available when running in fast mode, the default for JScript .NET. To compile a program from the command line that uses these properties, you must turn off the fast option by using /fast-. It is not safe to turn off the fast option in ASP.NET because of threading issues.


The following example illustrates the use of the index property. This function iterates a search string and prints out the index and lastIndex values for each word in the string.

var src : String = "The rain in Spain falls mainly in the plain.";
var re : RegExp = /\w+/g;
var arr : Array;
while ((arr = re.exec(src)) != null)
   print(arr.index + "-" + arr.lastIndex + "\t" + arr);

The output of this program is:

0-3     The
4-8     rain
9-11    in
12-17   Spain
18-23   falls
24-30   mainly
31-33   in
34-37   the
38-43   plain


Version 3

See Also

Regular Expression Syntax | exec Method

Applies To: RegExp Object

© 2015 Microsoft