RegExp Object property LastIndex

P

Patient Guy

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

Michael Winter

Patient said:
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
 
M

Martin Honnen

Patient said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top