archives

« Bugzilla Issues Index

#2617 — 21.2.5.7 RegExp.prototype.replace: Call replacer function after all matches were found


21.2.5.7 RegExp.prototype.replace ( string, replaceValue ):

For ECMAScript 5 and browser compatibility, RegExp.prototype.replace needs to call the replacer function after all matches were found.

Test case: https://github.com/mozilla/gecko-dev/blob/master/js/src/tests/ecma_5/String/replace-updates-global-lastIndex.js

Simplified test case:
---
r = /x/g;
'0x2x4x6x8'.replace(r, function(){ print(r.lastIndex) })
---

Expected: Prints four times 0
Actual: Prints 2, 4, 6, 8

Tested in: SpiderMonkey, JSC, V8, IE11, Nashorn


Different test case which is currently broken when following spec algorithm:
---
var g_rx = /test/g;
"test-string".replace(g_rx, function() { g_rx.lastIndex = 0; return "a" });
---

Expected: "a-string"
Actual: attempt to retrieve invalid substring from [start=4, end=0]


fixed in rev24 editor's draft


fixed in rev24