Skip to content

Instantly share code, notes, and snippets.

@catdad
Created August 22, 2017 19:26
Show Gist options
  • Save catdad/f61e3950dbe6f2bb2aa3b414cccd0330 to your computer and use it in GitHub Desktop.
Save catdad/f61e3950dbe6f2bb2aa3b414cccd0330 to your computer and use it in GitHub Desktop.
function renderMustache(str, obj) {
return Object.keys(obj).reduce(function (memo, key) {
var value = obj[key];
var regex = new RegExp('\\$\\{' + key + '\\}', 'g');
return memo.replace(regex, value);
}, str);
};
// usage
renderMustache('thing/${a}/stuff/${b}', { a:1, b:2 });
// > 'thing/1/stuff/2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment