I have a template in a String and I want to replace a few of the placeholders with the values that I have in another string. For every placeholder that I replace, I also want to insert a break tag.
For eg if #ADDRESS2# is found in the template, I am using the following code to replace all its occurrences with value in string val.address2.
template_html = template_html.replace(/#ADDRESS2#/g, '<br />'+ val.address_2);
However there are scenarios when the string val.address2 is empty. In that case, I do not want to insert the break tag.
So I changed my code as follows
if( val.address_2.length > 0 ) {
template_html = template_html.replace(/#ADDRESS2#/g, '<br />'+ val.address_2);
} else {
template_html = template_html.replace(/#ADDRESS2#/g, '');
}
Is there a better way to write the above code as I have multiple Placeholders and for each Placeholder I have to write the code 2 times.