Skip to content

Instantly share code, notes, and snippets.

@avishwakarma
Created June 28, 2016 05:57
Show Gist options
  • Select an option

  • Save avishwakarma/4b5ad727d216f6aeef2b2f4bd2b28e6d to your computer and use it in GitHub Desktop.

Select an option

Save avishwakarma/4b5ad727d216f6aeef2b2f4bd2b28e6d to your computer and use it in GitHub Desktop.
JavaScript replace multiple items in a string at once worked as str_replace PHP function
/**
* replaceAll - JavaScript string function
* @uses replace all matching substring to its corresponding replace string
* Worked similarly as str_replace in PHP
*/
String.prototype.replaceAll = function(find, replace) {
var replaceString = this;
var regex;
for (var i = 0; i < find.length; i++) {
regex = new RegExp(find[i], "g");
replaceString = replaceString.replace(regex, replace[i]);
}
return replaceString;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment