Skip to content

Instantly share code, notes, and snippets.

@sunpietro
Last active February 1, 2016 09:31
Show Gist options
  • Save sunpietro/ad0100f6e5665dc70ac5 to your computer and use it in GitHub Desktop.
Save sunpietro/ad0100f6e5665dc70ac5 to your computer and use it in GitHub Desktop.
Formats a string like sprintf
var formatString = (function()
{
var replacer = function(context)
{
return function(s, name)
{
return context[name];
};
};
return function(input, context)
{
return input.replace(/\{(\w+)\}/g, replacer(context));
};
})();
//usage
formatString("Hello {name}, {greeting}", {name: "Steve", greeting: "how's it going?"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment