I\'m attempting to set up a script to concatenate some variables inside a string if they exist, in order to place the appropriate metadata tags into a rendered HTML
I liked the readability of Demian Brecht answer, but I would only change the string for a regex instead, because the replace() function only replaces the first match (see more here: JavaScript .replace only replaces first Match)
var metadata_title = "Hello";
var metadata_author = "Me";
var metadata_date = "2011-09-07";
var template = "\
\
#title# \
\
\
\
\
\
";
var data = template.replace(/#title#/g, metadata_title != undefined ? metadata_title : "")
.replace(/#author#/g, metadata_author != undefined ? metadata_author : "")
.replace(/#date#/g, metadata_date != undefined ? metadata_date : "");