Created
September 13, 2013 08:46
-
-
Save miraculixx/6548200 to your computer and use it in GitHub Desktop.
Provide a generic format method for JavaScript strings. Use see fiddle http://jsfiddle.net/UkxE7/
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.format = function(string) { | |
args = arguments.length == 2 ? arguments[1] : arguments.slice(1); | |
return string.replace(/{(\w+)}/g, function(match, idx) { | |
return typeof args[idx] != "undefined" ? args[idx] : match; | |
}); | |
}; | |
String.prototype.format = function() { | |
return String.format(this, arguments); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment