Last active
December 20, 2015 01:09
-
-
Save acuppy/6047486 to your computer and use it in GitHub Desktop.
String#arrayReplace: similar to String#replace, but allows you to pass multiple array sets for replacement.
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
String.prototype.arrayReplace = (args...) -> | |
rtn = @ | |
for set in args | |
rtn = rtn.replace(new RegExp(set[0], 'gi'), set[1]) | |
rtn |
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
/* | |
Replace characters matching the first element of an array with the second element | |
"mississippi".arrayReplace(['i','n'], ['p','m']) => "mnssnssnmmn" | |
@params Array | |
@return String | |
*/ | |
String.prototype.arrayReplace = function() { | |
var args, rtn, set, _i, _len; | |
args = 1 <= arguments.length ? Array.prototype.slice.call(arguments, 0) : []; | |
rtn = this; | |
for (_i = 0, _len = args.length; _i < _len; _i++) { | |
set = args[_i]; | |
rtn = rtn.replace(new RegExp(set[0], 'gi'), set[1]); | |
} | |
return rtn; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment