Skip to content

Instantly share code, notes, and snippets.

@dun4n
Created March 5, 2014 10:43
Show Gist options
  • Save dun4n/9364972 to your computer and use it in GitHub Desktop.
Save dun4n/9364972 to your computer and use it in GitHub Desktop.
#JavaScript simple String match / replace from arguments
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