Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用正则表达式返回字符串中的特定模式?_Javascript_Node.js_Regex_String - Fatal编程技术网

如何在javascript中使用正则表达式返回字符串中的特定模式?

如何在javascript中使用正则表达式返回字符串中的特定模式?,javascript,node.js,regex,string,Javascript,Node.js,Regex,String,我有这样一个字符串: let temp = 'Hi {{username}}, Your request with request id: {{requestId}} has been processed. Please contact your nearest shop.' 我想要字符串中的此数组: ['userName','requestId'] 我知道我必须以某种方式使用正则表达式来实现这一点,但我无法找出实现这一点的模式 注意:这只是一个示例字符串,我想要一个更通用的方法来解决这个问题

我有这样一个字符串:

let temp = 'Hi {{username}}, Your request with request id: {{requestId}} has been processed. Please contact your nearest shop.'
我想要字符串中的此数组:

['userName','requestId']

我知道我必须以某种方式使用正则表达式来实现这一点,但我无法找出实现这一点的模式


注意:这只是一个示例字符串,我想要一个更通用的方法来解决这个问题,因为字符串可能会有所不同。您需要使用正向前瞻。这是适用于您的案例的正则表达式

let temp = 'Hi {{username}}, Your request with request id: {{requestId}} has been processed. Please contact your nearest shop.'
temp.match(/(?<={{)(\w+)(?=}})/g)
let temp='Hi{{username},您的请求id:{{{requestId}已被处理。请与最近的商店联系。”

temp.match(/(?关于
{(.*?}}}
。我没有任何javascript经验,但在某些语言上,它的输出带有括号。我不想要括号@D-E-n,您可以简单地添加一个捕获组:
{(.*?}}
请更新您的问题以匹配您接受的答案,或者取消接受答案。这不符合您的要求,字符串可能会有所不同。太好了!如果括号中的字符串中有空格,这将不匹配。您可以使用
/(?@Kritarth-OP-states)(粗体字体)括号内的字符串可能会有所不同。您的解决方案根本无法满足这一要求。即使它适用于OP,它也不会改变这样一个事实,即这是对所问问题的错误答案。而且不仅仅是空格。诸如
|
$
-
等字符将破坏此正则表达式。
$
是JavaScript变量名中的有效字符。