Created
June 9, 2022 15:20
-
-
Save dayitv89/0fe50875e12a1786e3090f5024fc608e to your computer and use it in GitHub Desktop.
javascript replaceAll for node version < 15
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
//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