Skip to content

Instantly share code, notes, and snippets.

@dchapkine
Last active January 4, 2016 19:29
Show Gist options
  • Save dchapkine/8667464 to your computer and use it in GitHub Desktop.
Save dchapkine/8667464 to your computer and use it in GitHub Desktop.
var replaceVarsUnsafe = function(vars, str, sep) {
sep = sep||'%';
for (var i in vars) str = str.replace(new RegExp('('+sep+i+sep+')+', 'g'), vars[i]);
return str;
};
var s = "/home/%user%/somedir/%dir1%/foo/%dir2%/%dir1%/file.%ext%";
r = replaceVarsUnsafe({
user: "alice",
dir1: "aaaaaaa",
dir2: "bbbbbbb",
ext: "png"
}, s);
console.log(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment