Created
April 15, 2016 15:29
-
-
Save lot224/ca1dc4520c671012e7fbc8d4b699fbc9 to your computer and use it in GitHub Desktop.
javascript string.format like c# (ish)
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
if (!String.prototype.format) { | |
String.prototype.format = function () { | |
var n = this; | |
for (var i = 0; i < arguments.length; i++) { | |
var e = new RegExp('\\{' + (i) + '\\}', 'gm'); | |
n = n.replace(e, arguments[i]); | |
} | |
return n; | |
} | |
} | |
if (!String.format) { | |
String.format = function () { | |
for (var i = 1; i < arguments.length; i++) { | |
var e = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); | |
arguments[0] = arguments[0].replace(e, arguments[i]); | |
} | |
return arguments[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment