Returns the character position where the next match begins in a searched string.
{RegExp | reArray}.lastIndex |
Arguments
- RegExp
-
Required. The global RegExp object.
- reArray
-
Required. An array returned by the exec method of a Regular Expression object.
Remarks
The lastIndex property is zero-based, that is, the index of the first character is zero. Its initial value is –1. Its value is modified whenever a successful match is made.
The lastIndex property of the RegExp object is modified by the exec and test methods of the RegExp object, and the match, replace, and split methods of the String object.
The following rules apply to values of lastIndex:
-
If there is no match, lastIndex is set to -1.
-
If lastIndex is greater than the length of the string, test and exec fail and lastIndex is set to -1.
-
If lastIndex is equal to the length of the string, the regular expression matches if the pattern matches the empty string. Otherwise, the match fails and lastIndex is reset to -1.
-
Otherwise, lastIndex is set to the next position following the most recent match.
The initial value of the RegExp.lastIndex 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. 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. |
Example
The following example illustrates the use of the lastIndex property. This function iterates a search string and prints out the index and lastIndex values for each word in the string.
В | Copy Code |
---|---|
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:
В | Copy Code |
---|---|
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 |