Created
January 15, 2013 21:56
-
-
Save mcheck/4542499 to your computer and use it in GitHub Desktop.
javascript string interpolation ... sort of
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
// javascript string interpolation | |
// simple string builder- usage: stringFormat("Hello {0}","world"); | |
// returns "Hello world" | |
function stringFormat() { | |
var s = arguments[0]; | |
for (var i = 0; i < arguments.length - 1; i++) { | |
var reg = new RegExp("\\{" + i + "\\}", "gm"); | |
s = s.replace(reg, arguments[i + 1]); | |
} | |
return s; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment