Created
August 14, 2012 10:07
-
-
Save andrijac/3347962 to your computer and use it in GitHub Desktop.
.net like format function for strings in javascript
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
var formatFunc = function format() { | |
// slice 1st parameter which is pattern | |
var args = Array.prototype.slice.call(arguments, 1), | |
pattern = arguments[0]; | |
return pattern.replace(/{(\d+)}/g, function (match, number) { | |
return typeof args[number] != 'undefined' ? args[number] : match; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment