这里我想在Text中找到单词的匹配文本模式

问题描述 投票:0回答:1

您能帮我解决一下吗?我有两种类型的代码;其中一个正在运行,另一个则不起作用。

工作

const str =
'Hi there, my name is \[name\], I am \[age\] years old, and I work in 
the field of \[profession\].';

const matches = str.match(/\[.+?\]/g);
console.log(matches);

不工作

let re = new RegExp(`/\[.+?\]/`, 'g');
let isMatch = str.match(re);

console.log(isMatch);
regex regression regex-lookarounds regex-group
1个回答
0
投票

您需要在构造函数调用中的字符串定义中添加另一个反斜杠:

new RegExp()

© www.soinside.com 2019 - 2024. All rights reserved.