Share via


index Property

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

{RegExp | reArray}.index

Arguments

  • RegExp
    Required. The global RegExp object.

  • reArray
    Required. An array returned by the exec method of a Regular Expression object.

Remarks

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 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.

Example

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:

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

Requirements

Version 3

Applies To:

RegExp Object

See Also

Concepts

Regular Expression Syntax

Reference

exec Method

Change History

Date

History

Reason

July 2009

Modified example.

Content bug fix.

July 2009

Modified note about the /fast option.

Content bug fix.

March 2009

Modified example and information about the /fast option.

Information enhancement.