User Guide

302 Using Regular Expressions
The lastIndex property
The lastIndex property specifies the index position in the string at which to start the next
search. This property affects the
exec() and test() methods called on a regular expression
that has the
g flag set to true. For example, consider the following code:
var pattern:RegExp = /p\w*/gi;
var str:String = "Pedro Piper picked a peck of pickled peppers.";
trace(pattern.lastIndex);
var result:Object = pattern.exec(str);
while (result != null)
{
trace(pattern.lastIndex);
result = pattern.exec(str);
}
The lastIndex property is set to 0 by default (to start searches at the beginning of the string).
After each match, it is set to the index position following the match. Therefore, the output for
the preceding code is the following:
0
5
11
18
25
36
44
If the global flag is set to false, the exec() and test() methods do not use or set the
lastIndex property.
The
match(), replace(), and search() methods of the String class start all searches from
the beginning of the string, regardless of the setting of the
lastIndex property of the regular
expression used in the call to the method. (However, the
match() method does set
lastIndex to 0.)
You can set the
lastIndex property to adjust the starting position in the string for regular
expression matching.
The source property
The source property specifies the string that defines the pattern portion of a regular
expression. For example:
var pattern:RegExp = /foo/gi;
trace(pattern.source); // foo