Skip to content

Instantly share code, notes, and snippets.

@marclundgren
Created December 22, 2014 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marclundgren/7db4a6b6984c9fdfe198 to your computer and use it in GitHub Desktop.
Save marclundgren/7db4a6b6984c9fdfe198 to your computer and use it in GitHub Desktop.
// Sub utility
var sub = function(str, obj) {
var keys = Object.keys(obj);
keys.map(function(key) {
var re = new RegExp('{' + key + '}', 'gm');
str = str.replace(re, obj[key]);
});
return str;
};
module.exports = sub;
@marclundgren
Copy link
Author

var sub = require("sub");

var template = "My favourite month is {month}";

sub(template, {
  month: "December"
});

// My favourite month is December

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment