Fix String.prototype.split on Safari.

13 views
Skip to first unread message

gugod

unread,
Dec 24, 2006, 4:35:12 AM12/24/06
to hsinchu.js
Here's a quick and dirty fix for String.prototype.split function to let
it support captured regex seperator.
It overrides the original String.prototype.split and is not completed
behaving the same as mozilla's version of split function, use it with
caution. You are warned.

-----8<-----

var ua = navigator.userAgent.toLowerCase()

if (ua.indexOf("safari/") != -1) {
var safari_split = String.prototype.split;
var my_split = function(seperator) {
var result = [];

if (seperator.constructor() == '//') {
var capture = false;
var pattern =
seperator.toString().replace(/(^\/|\/$)/g,'');
if ( pattern.match(/^\(/) ) {
capture = true;
}

pattern = "^(.*?)(" + pattern + ")";

var rp = new RegExp(pattern);

var str = this;
while(str.length > 0 ) {
var r = rp.exec(str)
if ( r ) {
result.push( r[1] );
if ( capture ) {
result.push( r[2] )
}
str = str.substr(r[0].length);
}
else {
result.push( str );
str = ""
}
}
}
else {
return safari_split.apply(this, arguments);
}

return result
}
String.prototype.split = my_split;
}

Reply all
Reply to author
Forward
0 new messages