Skip to content

Instantly share code, notes, and snippets.

@jmelosegui
Last active September 12, 2016 16:32
Show Gist options
  • Save jmelosegui/8aaab0f21d48f85956e4ab9c72b3feb1 to your computer and use it in GitHub Desktop.
Save jmelosegui/8aaab0f21d48f85956e4ab9c72b3feb1 to your computer and use it in GitHub Desktop.
Mimicking the .Net string format.
/*
This will format a string mimicking the .Net string format.
ie: format('test {0} - {1}', 'value 1', 'value 2');
*/
function format(value) {
var args = Array.prototype.slice.call(arguments, 1);
return value.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