By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

RegExp Object property LastIndex

P: n/a

Given the following code and its execution to the last line:

var re = /beta/;
var string = "alphabetagamma";
var report = re.exec(string);

should not:

re.lastIndex = 9;

after the last line of execution?

On Firefox, Venkman continually reports zero.

Specification for JS 1.5 defines property lastIndex as follows:

The index at which to start the next match.
As of JavaScript 1.5, [it is] a property of a
RegExp instance, not the RegExp object.

Sep 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Patient Guy wrote:
var re = /beta/;
var string = "alphabetagamma";
var report = re.exec(string);

should not:

re.lastIndex = 9;

after the last line of execution?
No. The lastIndex property is only changed if the global flag is set
(that is, /beta/g).

The index property of the result array indicates where the matched
substring starts; in the example above, report.index will evaluate to 5.

[snip]

Mike
Sep 6 '06 #2

P: n/a


Patient Guy wrote:
Given the following code and its execution to the last line:

var re = /beta/;
var string = "alphabetagamma";
var report = re.exec(string);

should not:

re.lastIndex = 9;

after the last line of execution?
Use the g flag e.g.
var re = /beta/g;
if you want the exec method to report lastIndex and start from there on
the next call.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.