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 wan
If the same logic should be applied for multiple address fields, then you might benefit from a helper function:
template_html = template_html
.replace(/#CITY1#/g, PrefixBrIfNotEmpty(val.city_1))
.replace(/#CITY2#/g, PrefixBrIfNotEmpty(val.city_2))
.replace(/#CITY3#/g, PrefixBrIfNotEmpty(val.city_3))
.replace(/#ADDRESS1#/g, PrefixBrIfNotEmpty(val.address_1))
.replace(/#ADDRESS2#/g, PrefixBrIfNotEmpty(val.address_2))
.replace(/#ADDRESS3#/g, PrefixBrIfNotEmpty(val.address_3));
function PrefixBrIfNotEmpty(str) {
return str ? '
' + str : '';
}