Created
March 5, 2014 10:43
-
-
Save dun4n/9364972 to your computer and use it in GitHub Desktop.
#JavaScript simple String match / replace from arguments
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.replacer = function() { | |
var args = arguments | |
return this.replace(/\{([0-9]*)\}/g, function(match, parameter) { | |
return args[parseInt(parameter)] | |
}) | |
} | |
var hello = "Hello {0} {1}!" | |
hello.replacer("John", "Doe") // "Hi John Doe!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment