Skip to content

Instantly share code, notes, and snippets.

@andrijac
Created August 14, 2012 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrijac/3347962 to your computer and use it in GitHub Desktop.
Save andrijac/3347962 to your computer and use it in GitHub Desktop.
.net like format function for strings in javascript
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