This is a bug in V8 (bug 1748):
http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/regress/regress-1748.js?spec=svn9504&r=9504
Here is the error test:
function assertEquals(a, b, msg) { if(a !== b) { console.log("'%s' != '%s' %s", a, b, msg); } } var str = Array(10000).join("X"); str.replace(/^|X/g, function(m, i, s) { if (i > 0) assertEquals("X", m, "at position 0x" + i.toString(16)); });
In my window, it prints:
'X'! = ''. at position 0x100
'X'! = ''. at position 0x200
'X'! = ''. at position 0x300
'X'! = ''. at position 0x400
'X'! = ''. at position 0x500
'X'! = ''. at position 0x600
...
It does not print anything in jsfiddle (the V8 version in the Chrome browser has no error):
http://jsfiddle.net/PqDHk/
Error History:
From V8 changelog, the bug was fixed in V8-3.6.5 (2011-10-05).
From Node.js changelog , Node -0.6.5 should use V8-3.6.6.11!? !!?. Node.js was upgraded from V8-3.6.4 to V8-3.7.0 (Node -0.5.10), and then downgraded to V8-3.6.6 for Node -0.6.0. Theoretically, this error should be fixed before Node V0.6.0. Why is it still playing on Node -0.6.5 ??? Odd
Can someone with the latter (Node -0.6.15) run the test fragment above and report if it generates errors? Or I will eventually get along.
Thanks to ZachB for confirming this error on Node -0.6.15. I registered a problem ( issue # 3168 ) against node, and the fix ( 5d69bbf ) should be included in Node -0.6.16. :) :) :)
Until then, the workaround is to replace:
str.replace(/^/, indent);
FROM
str.replace(/(^|\n)/, "$1" + indent);
UPDATE: just for a giggle, I checked this on the current version of Node v0.8.1 and confirmed that the error had indeed been fixed. I did not bother to go back and confirm whether the error in 0.6.16 was fixed or once between them and next to v0.8.X.