Skip to content

Instantly share code, notes, and snippets.

@webbower
Created July 15, 2015 22:46
Show Gist options
  • Save webbower/6e458d4d6178c13a923f to your computer and use it in GitHub Desktop.
Save webbower/6e458d4d6178c13a923f to your computer and use it in GitHub Desktop.
Simple string formatting function in JS
// 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