In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma?

后端 未结 17 1642
滥情空心
滥情空心 2020-12-02 10:26

I am using the Mustache templating library and trying to generate a comma separated list without a trailing comma, e.g.

red, green, blue

17条回答
  •  一个人的身影
    2020-12-02 10:50

    For JSON data I suggest:

    Mustache.render(template, settings).replace(/,(?=\s*[}\]])/mig,'');

    The regexp will remove any , left hanging after the last properties.

    This will also remove , from string values contining ", }" or ", ]" so make sure you know what data will be put into your JSON

提交回复
热议问题