Last active
December 14, 2015 07:18
-
-
Save masawada/5049042 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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