Closed Bug 423155 Opened 16 years ago Closed 16 years ago

JavaScript: Calling exec multiple times in a loop.

Categories

(Firefox :: General, defect)

PowerPC
macOS
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 98409

People

(Reporter: craig, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-GB; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4
Build Identifier: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-GB; rv:1.9b4) Gecko/2008030317 Firefox/3.0b4

Not sure how to explain this... add the following to a JavaScript file:

--------------
for (var k = 0; k < 5; k++) {
	alert(/t/g.exec('t') ? 'A' : 'B');
}
--------------

When the code is executed, the alert windows show "A,B,A,B,A"... they should always return the same value, as the same regular expression is called on the same string.

Taking away the 'global' matching fixes this... but it should work with it as well... for example, I use a regular expression to find all tags within a string (match), but it uses the same regular expression to extract the parts of the match, within the loop (exec).

Reproducible: Always
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.