[英]How to get multiple strings between two other strings
I am trying to find all values between {{
and }}
that start with a $
followed by [\\w.]+
but doesn't start with a number.我试图找到
{{
和}}
之间以$
开头的所有值,后跟[\\w.]+
但不以数字开头。 This works in getting the value, however, it only gets the last item.这适用于获取值,但是,它只获取最后一项。 What I am looking for, is a list of these items, so in this example I would like my array to contain (based off the example):
我正在寻找的是这些项目的列表,因此在此示例中,我希望我的数组包含(基于示例):
['{{I\'m $name and I am $age years old}}', '$name', '$age']
When executed, $name
is not included in this array as seen here:执行时,
$name
不包含在此数组中,如下所示:
let result = '$welcome: {{I\\'m $name and I am $age years old}}'.match(/\\{\\{.*(\\$(?!\\d|\\.)[\\w.]+).*\\}\\}/) console.log(result)
You could try the below regex:你可以试试下面的正则表达式:
\$[.\w]+(?=[^{}]*})
This doesn't really match between {{
and }}
but it guarantees that it wouldn't find a match which is outside of braces.这在
{{
和}}
之间并不真正匹配,但它保证它不会找到大括号之外的匹配项。 It assumes that single, unbalanced braces do not occur in input string.它假设输入字符串中不会出现单个不平衡的大括号。
Another approach would be matching an entire {{...}}
block then looking for \\$[\\w.]+
but if you need to replace them in original input string this wouldn't be an easy option.另一种方法是匹配整个
{{...}}
块然后查找\\$[\\w.]+
但如果您需要在原始输入字符串中替换它们,这将不是一个简单的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.