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



我有一个这样的字符串:

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)

最新更新