Skip to content

Instantly share code, notes, and snippets.

@tetri
Last active August 29, 2015 14:06
Show Gist options
  • Save tetri/6d69863534221cd3dc66 to your computer and use it in GitHub Desktop.
Save tetri/6d69863534221cd3dc66 to your computer and use it in GitHub Desktop.
/*
based on http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript
according to http://jsperf.com/replace-all-vs-split-join/25
*/
if (!String.prototype.replaceAll)
String.prototype.replaceAll = function (str, find, rep) {
return str.replace(new RegExp(find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"), 'g'), rep); //faster for Firefox
return str.split(find).join(rep); //faster for Chrome
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment