Created
June 28, 2016 05:57
-
-
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
This file contains hidden or 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
| /** | |
| * 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