您能帮我解决一下吗?我有两种类型的代码;其中一个正在运行,另一个则不起作用。
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);
您需要在构造函数调用中的字符串定义中添加另一个反斜杠:
new RegExp()