Skip to content

Instantly share code, notes, and snippets.

@masawada
Last active December 14, 2015 07:18
Show Gist options
  • Save masawada/5049042 to your computer and use it in GitHub Desktop.
Save masawada/5049042 to your computer and use it in GitHub Desktop.
String.prototype.replace = function(re, str){
var replace = function(t, b, a){
var i = t.indexOf(b), n = i+b.length-t.length;
return (i !== -1)? t.slice(0,i) + a + ((n<0)? t.slice(n) : '') : t;
}, ret;
if(typeof re === 'string'){
ret = replace(this, re, str);
}else if(re instanceof RegExp){
ret = this; var ba = ret.match(re), l = ba.length, i;
for(i=0;i<l;i++){ret = replace(ret, ba[i], str);}
}else{
ret = this;
}
return ret.toString();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment