Created
July 15, 2015 22:46
-
-
Save webbower/6e458d4d6178c13a923f to your computer and use it in GitHub Desktop.
Simple string formatting function in JS
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
// Take a string template with {var1} placeholders and replace them with | |
// matching keys from POJO | |
function format(str, data) { | |
data = Object(data); | |
var re, p; | |
for (p in data) { | |
if (has(data, p)) { | |
re = new RegExp('\\\{' + p + '\\\}', 'g'); | |
str = str.replace(re, data[p]); | |
} | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment