Fix String#replace to support functions in Safari 2
Reported by fearphage | August 22nd, 2008 @ 11:13 AM
Birthed from #297
Safari 2.x does not support functions as the 2nd param of String#replace. The following code attempts to remedy this:
if ('a'.replace(/a/, function() {return 'b'}) != 'b')
(function(replace){
String.prototype.replace = function(pattern, replacement) {
// replacement is not function, use built-in
if (typeof replacement != 'function')
return replace.apply(this, arguments);
var str = '' + this;
// pattern string is not RegExp
if (!(pattern instanceof RegExp)) {
var idx = str.indexOf(pattern);
return (idx == -1 ?
str :
replace.apply(str, [pattern, replacement(pattern, idx, str)]);
);
}
var reg = pattern, result = [], lastidx = reg.lastIndex, re;
while ((re = reg.exec(str)) != null) {
var idx = re.index, args = re.concat(idx, str);
result.push(str.slice(lastidx, idx), replacement.apply(null, args).toString());
if (!reg.global) {
lastidx += RegExp.lastMatch.length;
break;
}
else {
lastidx = reg.lastIndex;
}
}
result.push(str.slice(lastidx));
return result.join('');
}
})(String.prototype.replace);
The exchange is performance (failing quickly with the built-in method) for functionality (working as spec'd).
Comments and changes to this ticket
-
Tobie Langel August 22nd, 2008 @ 11:58 AM
- → State changed from new to enhancement
-
Tobie Langel August 22nd, 2008 @ 12:20 PM
- → Tag changed from blocker297 enhancement needs_benchmarks needs_patch needs_tests safari2 string to needs_docs needs_patch needs_tests safari2 string
This actually might be only a Safari 1.2 bug.
-
Juriy Zaytsev August 22nd, 2008 @ 02:00 PM
javascript:alert('a'.replace(/a/,function(){return 'b'})); // Safari 2.0 function(){return 'b'}; // Safari 2.0.2 function(){return 'b'}; // Safari 2.0.3 'b'
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
The Prototype JavaScript library.
