Skip to content

Instantly share code, notes, and snippets.

@andrijac
Last active November 14, 2019 14:16
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/ef4ac4340884d319b41c9b7a276bf037 to your computer and use it in GitHub Desktop.
Save andrijac/ef4ac4340884d319b41c9b7a276bf037 to your computer and use it in GitHub Desktop.
function stringFormat(format, args) {
if (arguments.length === 0) {
throw "Missing arguments";
}
if (!args) {
return format;
}
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
}
stringFormat("{0} {1} {2}", ["1", "22"])
// "1 22 {2}"
stringFormat("asd")
// "asd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment