如何使用正则表达式将String.match()与$ {SOME_TEXT}区分开来

Tha*_*s P 1 javascript regex string string-matching

我需要这个字符串:

var x = 'Hi ${name}! How are you? ${name}, you are old! ${name} share with ${other} how do u ${feel}!'
Run Code Online (Sandbox Code Playgroud)

我需要知道使用正则表达式存在多少不同的$ {ANY_THING}.在上面的示例中,我预计3:$ {name},$ {other},$ {feel}

我正在尝试:

x.match(\${([a-zA-Z]))
Run Code Online (Sandbox Code Playgroud)

但输出是错误的:(

谢谢!

zer*_*kms 5

我需要知道使用正则表达式存在多少不同的$ {ANY_THING}

x.match(/\$\{[^\}]+\}/g)
 .sort()
 .filter(function(element, index, array) {
     return index == array.indexOf(element);
 }) // this .filter() filters out the duplicates (since JS lacks of built in
    // unique filtering functions
 .length;
Run Code Online (Sandbox Code Playgroud)

上面的代码将返回3,因为x字符串中有多少个不同的项.

JSFiddle:http://jsfiddle.net/cae6P/

PS:仅使用正则表达式是不可能的.您需要使用.filter()解决方案或其他类似方法过滤重复项