Skip to content

Instantly share code, notes, and snippets.

@jancassio
Last active September 26, 2015 00:28
Show Gist options
  • Save jancassio/1010329 to your computer and use it in GitHub Desktop.
Save jancassio/1010329 to your computer and use it in GitHub Desktop.
Very simple string format in JavaScript
/**
* Simple string format.
* @author: Jan Cássio | [email protected]
* @usage
* "{0} World, my name is {1}".format("Hello", "Jan"); // returns "Hello world, my name is Jan";
*/
String.prototype.format = function()
{
var i, result, pattern;
result = this;
for (i = 0; i < arguments.length; i++)
{
pattern = new RegExp('\\{'+i+'\\}', 'gi');
result = result.replace(pattern, arguments[i]);
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment