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 a program is running in fast mode, the default for JScript. To compile a program that uses these properties from a command prompt, 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. The code iterates through a search string and prints out the index and lastIndex values for each word in the string.

var src = "The quick brown fox jumps over the lazy dog.";

// Create regular expression pattern with a global flag.
var re = /\w+/g;

// Get the next word, starting at the position of lastIndex.
var arr;
while ((arr = re.exec(src)) != null)
    {
    print (arr.index + "-" + arr.lastIndex + " " + arr);
    }

The output of this program is as follows.

0-3 The
4-9 quick
10-15 brown
16-19 fox
20-25 jumps
26-30 over
31-34 the
35-39 lazy
40-43 dog
Show: