如何使用正则表达式 String.match() 区分 ${SOME_TEXT} [英] How to String.match() distinct it ${SOME_TEXT} using Regex
本文介绍了如何使用正则表达式 String.match() 区分 ${SOME_TEXT}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这个字符串:
var x = 'Hi ${name}! How are you? ${name}, you are old! ${name} share with ${other} how do u ${feel}!'
我需要使用正则表达式知道存在多少不同的 ${ANY_THING}.在上面的例子中,我期望 3: ${name}, ${other}, ${feel}
I need to know using Regex how much distinct ${ANY_THING} exists. In example above i expect 3: ${name}, ${other}, ${feel}
我正在尝试:
x.match(\${([a-zA-Z]))
但是输出是错误的:(
谢谢!
推荐答案
我在 #regex IRC Channel 找到了这个解决方案,由 farn 用户提供:
I find this solution at #regex IRC Channel by farn user:
x.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g);
输出:
['${name}',
'${other}',
'${feel}']
和
x.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g).length;
输出:
3
:)
这篇关于如何使用正则表达式 String.match() 区分 ${SOME_TEXT}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文