[Webkit-unassigned] [Bug 45959] String.prototype.replace passes undefined rather than empty string for groups that don't match

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Sun Sep 19 01:07:06 PDT 2010


https://bugs.webkit.org/show_bug.cgi?id=45959





--- Comment #6 from Mark Hahnenberg <mhahnenb at gmail.com>  2010-09-19 01:07:05 PST ---
Unfortunately Firefox and IE appear to do different things:

Firefox 4.0b6 - passes empty string
IE8/9 - passes undefined
Opera 10.62 - passes undefined

I looked a little more closely at the ECMA-262 spec.  When it describes how RegExp.prototype.exec should behave, it explicitly states than any non-matched capture groups should be considered undefined.  It seems intuitive that when String.prototype.replace is passed a function for replaceValue, it would do something along the lines of:

1) Run the regex specified in the searchValue argument on the receiver of the method call ("this") by calling exec(), e.g. "searchValue.exec(this);"
2) Take the elements of the resulting array from the exec() call and pass them into replaceValue along with the other required arguments.
3) Replace the matched substrings with the return value of the function, converting that returned value to a String if it's not already.

Since the behavior of replace() seems to align with the behavior of exec(), perhaps they should work similarly to avoid cognitive dissonance between the two.  Obviously, the implementation doesn't have to actually work like this, but it seems like a fairly intuitive way of thinking about it (if I understand the spec correctly, that is :-)

>From the ECMA-262 spec for RegExp.prototype.exec:

"If choices in the left Alternative are exhausted, the right Disjunction is tried instead of the left Alternative. Any capturing parentheses inside a portion of the pattern skipped by | produce undefined values instead of Strings. Thus, for example,
/a|ab/.exec("abc")
returns the result "a" and not "ab". Moreover,
/((a)|(ab))((c)|(bc))/.exec("abc")
returns the array
["abc", "a", "a", undefined, "bc", undefined, "bc"]
and not
["abc", "ab", undefined, "ab", "c", "c", undefined]"

-- 
Configure bugmail: https://bugs.webkit.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.



More information about the webkit-unassigned mailing list