This documentation is archived and is not being maintained.

index Property 

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


{RegExp | reArray}.index

RegExp

Required. The global RegExp object.

reArray

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.

NoteNote

The properties of the RegExp object are not available when running in fast mode, the default for JScript. 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
Show: