Skip to content

Instantly share code, notes, and snippets.

@dayitv89
Created June 9, 2022 15:20
Show Gist options
  • Save dayitv89/0fe50875e12a1786e3090f5024fc608e to your computer and use it in GitHub Desktop.
Save dayitv89/0fe50875e12a1786e3090f5024fc608e to your computer and use it in GitHub Desktop.
javascript replaceAll for node version < 15
//node version < 15
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (str, newStr) {
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.split(str).join(newStr);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment