Is this a bug? Nonmatching capturing parens in regexp in replace with function are not undefined in

B

Bart Lateur

In this code:

function see(wot) {
var a = "0: " + arguments[0];
for (var i = 1; i < arguments.length; i++)
a += "\n" + i + ": " + arguments;
alert(a);
return wot;
}

"There is food at the bar".replace(/(foo)|(bar)/g, see)

In Firefox 2, you see 2 alert boxes with respective contents:

0: foo
1: foo
2:
3: 9
4: There is food at the bar

0: bar
1:
2: bar
3: 21
4: There is food at the bar

The non-matching capture (arguments[2] in the first case, arguments[1]
in the second) is the empty string, but defined. That makes it very hard
to see if a capture did match the empty string, as opposed to *not*
matching.

In MSIE6 and Opera9 I see almost the same, but with an essenstial
difference:

0: foo
1: foo
2: undefined
3: 9
4: There is food at the bar

0: bar
1: undefined
2: bar
3: 21
4: There is food at the bar

If you turn it inside out, with

/(foo)|(bar)/.exec("There is food at the bar")

Then, in Firebug, you see:

["foo", "foo", undefined]

which is the proper result.

Safari 3 beta appears to show the same results as Firefox.

What do you think... bug? I think it is... Where to report it?
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top