Javascript Replace - Dynamic Value of Replacement

后端 未结 7 1664
半阙折子戏
半阙折子戏 2020-12-21 10:58

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

7条回答
  •  时光取名叫无心
    2020-12-21 11:09

    You can use ternary operator (empty string evaluates to false)

    template_html = template_html.replace(/#ADDRESS2#/g, val.address_2 ? '
    '+ val.address_2 : '');

提交回复
热议问题