Last active
September 26, 2015 00:28
-
-
Save jancassio/1010329 to your computer and use it in GitHub Desktop.
Very simple string format in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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