Created
August 31, 2013 23:31
-
-
Save Kein1945/6401324 to your computer and use it in GitHub Desktop.
Javascript String format
This file contains hidden or 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
String.prototype.format = function() { | |
var i = -1, args = arguments; | |
return this.replace(/#\{(.*?)\}/g, function(one, two) { | |
return (typeof args[0] == 'object')?args[0][two]:args[++i]; | |
}); | |
} | |
var thing = 'world!'; | |
console.log( | |
'hello #{1} - #{2}'.format(thing, 'two!'), | |
'hello #{wrd} - #{2}'.format({'wrd': thing, '2': 'two!'}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment