Returns the character position where the next match begins in a searched string.
RegExp.lastIndex |
Remarks
The object associated with this property is always the global RegExp object.
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 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.
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 | |
---|---|
function RegExpTest(){ var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion()) if (ver >= 5.5){ var src = "The rain in Spain falls mainly in the plain."; var re = /\w+/g; var arr; while ((arr = re.exec(src)) != null) print(arr.index + "-" + arr.lastIndex + "\t" + arr); } else{ alert("You need a newer version of JScript for this to work"); } } |
Requirements
Applies To: RegExp Object (JScript 5.6)