Closed Bug 1034950 Opened 10 years ago Closed 10 years ago

Passing a function to String.prototype.replace, the function receives "" (empty string) instead of undefined for capture groups that haven't been matched

Categories

(Core :: JavaScript Engine, defect)

30 Branch
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 369778
Tracking Status
firefox30 --- affected
firefox31 --- affected
firefox32 --- affected
firefox33 --- affected

People

(Reporter: mdn, Unassigned)

References

()

Details

(Keywords: testcase)

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.
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.
Flags: needinfo?(mdn)
(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.
Flags: needinfo?(mdn)
Can you please attach a short html testcase ?
(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 )
Flags: needinfo?(mdn)
(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.
Flags: needinfo?(mdn)
(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/
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>
Component: Untriaged → JavaScript Engine
Keywords: testcase
OS: Mac OS X → All
Product: Firefox → Core
Hardware: x86 → All
Summary: Incorrect empty captures in MatchResult → Passing a function to String.prototype.replace, the function receives "" (empty string) instead of undefined for capture groups that haven't been matched
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? :-)
Flags: needinfo?(kvijayan)
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).
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Flags: needinfo?(kvijayan)
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.