Last Comment Bug 1034950 - Passing a function to String.prototype.replace, the function receives "" (empty string) instead of undefined for capture groups that haven't been matched
: Passing a function to String.prototype.replace, the function receives "" (emp...
Status: RESOLVED DUPLICATE of bug 369778
: testcase
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: 30 Branch
: All All
-- normal (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
:
: Jason Orendorff [:jorendorff]
Mentors:
data:text/html;charset=utf-8,<p>%0D%0...
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2014-07-06 01:16 PDT by mdn
Modified: 2014-07-08 06:12 PDT (History)
6 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---
affected
affected
affected
affected


Attachments

Description User image mdn 2014-07-06 01:16:18 PDT
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 OPR/22.0.1471.70

Steps to reproduce:

As per ECMAScript Spec 262 under paragraph 15.10.2.3, the expression

/((a)|(ab))((c)|(bc))/.exec("abc")

should return

["abc", "a", "a", undefined, "bc", undefined, "bc"]


Actual results:

In fact though, in Firefox it returns

["abc", "a", "a", "", "bc", "", "bc"]


Expected results:

All browsers except Firefox behave as expected.
Comment 1 User image :Gijs 2014-07-06 14:52:26 PDT
What version of Firefox did you test? Your testcase produces the desired results in latest nightly, latest beta (31), latest release (30) and in the slightly older 28.
Comment 2 User image mdn 2014-07-07 14:07:28 PDT
(In reply to :Gijs Kruitbosch from comment #1)
> What version of Firefox did you test?

Firefox 30.0 on OS X 10.9.4

> Your testcase produces the desired
> results in latest nightly, latest beta (31), latest release (30) and in the
> slightly older 28.

In fact, in Windows I'm getting correct results, so the issue may be specific to OS X.
Comment 3 User image Matthias Versen [:Matti] 2014-07-07 14:09:36 PDT
Can you please attach a short html testcase ?
Comment 4 User image :Gijs 2014-07-07 14:16:13 PDT
(In reply to mdn from comment #2)
> (In reply to :Gijs Kruitbosch from comment #1)
> > What version of Firefox did you test?
> 
> Firefox 30.0 on OS X 10.9.4
> 
> > Your testcase produces the desired
> > results in latest nightly, latest beta (31), latest release (30) and in the
> > slightly older 28.
> 
> In fact, in Windows I'm getting correct results, so the issue may be
> specific to OS X.

I tested on OS X. It works for me. Can you try with a clean profile? ( https://support.mozilla.org/kb/profile-manager-create-and-remove-firefox-profiles )
Comment 5 User image mdn 2014-07-07 14:23:32 PDT
(In reply to Matthias Versen [:Matti] from comment #3)
> Can you please attach a short html testcase ?

It was a bad example, my wrong. But here's one that produces the unexpected empty string in FF:

http://jsfiddle.net/QdBsn/

Hope you can reproduce this one.
Comment 6 User image mdn 2014-07-07 14:28:05 PDT
(In reply to :Gijs Kruitbosch from comment #4)
> (In reply to mdn from comment #2)
> > (In reply to :Gijs Kruitbosch from comment #1)
> > > What version of Firefox did you test?
> > 
> > Firefox 30.0 on OS X 10.9.4
> > 
> > > Your testcase produces the desired
> > > results in latest nightly, latest beta (31), latest release (30) and in the
> > > slightly older 28.
> > 
> > In fact, in Windows I'm getting correct results, so the issue may be
> > specific to OS X.
> 
> I tested on OS X. It works for me. Can you try with a clean profile? (
> https://support.mozilla.org/kb/profile-manager-create-and-remove-firefox-
> profiles )

I testes again with a clean profile and got the same results. Here's a correct fiddle demonstrating the issue: http://jsfiddle.net/QdBsn/
Comment 7 User image :Gijs 2014-07-07 14:45:11 PDT
The testcase in the URL field shows "undefinedc" in Chrome, and "c" in Firefox.

Source:

<p>
<script>
document.write("ac".replace(/a(b)?/, function(s, m) { return m + "" }));
</script>
</p>
Comment 8 User image :Gijs 2014-07-07 14:48:35 PDT
I'm not actually personally sure this is a bug per spec, despite reading the spec for String.prototype.replace and Regexp.prototype.exec 3 times - and if it is a bug, if we shouldn't change the spec because "" seems nicer as a function parameter than |undefined|, but I'll leave that for more enlightened minds to decide on.

Kannan, do you know, and/or do you know someone who knows? :-)
Comment 9 User image André Bargull 2014-07-07 15:23:49 PDT
Yes, this is a bug. Just like `"ac".match(/a(b)?/)` is supposed to return `[ "a", (void 0) ]`, the 1th capture in `"ac".replace(/a(b)?/, (s, m) => m + "")` is supposed to be `undefined`, which means the result string needs to be `"undefinedc"`. 

15.5.4.11 String.prototype.replace:
> If replaceValue is a function, then for each matched substring, call the function with the following
> m + 3 arguments. Argument 1 is the substring that matched. If searchValue is a regular expression, 
> the next m arguments are all of the captures in the MatchResult (see 15.10.2.1).
Comment 10 User image Till Schneidereit [till] 2014-07-08 06:12:49 PDT

*** This bug has been marked as a duplicate of bug 369778 ***

Note You need to log in before you can comment on or make changes to this bug.