Created
November 14, 2011 20:50
-
-
Save orhanveli/1365115 to your computer and use it in GitHub Desktop.
c# like string.format() function for 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
String.prototype.format = function () { | |
var s = this, | |
i = arguments.length; | |
while (i--) { | |
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); | |
} | |
return s; | |
}; | |
//useage | |
var output = 'Hello {0}, how are you today? if you {1} let me know :)'.format('Orhan', 'not ok'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment