在正则表达式中使用变量

时间:2021-01-24 14:01:22

标签: javascript typescript

我有变量标签

const label = 'test'

和正则表达式

{ name: /test/i }

我想要的是知道我是否可以在正则表达式中使用变量 label,就像这样

{ name: `/${label}/i` }

有可能吗?

1 个答案:

答案 0 :(得分:1)

要在正则表达式中使用变量,请使用:

new RegExp(`${label}`, 'i')