How to String.match() distinct it ${SOME_TEXT} using Regex

后端 未结 3 1087
轮回少年
轮回少年 2021-01-16 06:26

I need this string:

var x = \'Hi ${name}! How are you? ${name}, you are old! ${name} share with ${other} how do u ${feel}!\'

I need to know

3条回答
  •  花落未央
    2021-01-16 06:35

    I find this solution at #regex IRC Channel by farn user:

    x.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g);
    

    output:

    ['${name}',
     '${other}',
     '${feel}']
    

    and

    x.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g).length;
    

    output:

    3
    

    :)

提交回复
热议问题