正则表达式中的插值

aph*_*log -2 javascript typescript

我需要在正则表达式中添加插值。

我现在所拥有的就是var re = /212.129.52.45/gi 我需要的是将其替换212.129.52.45ip_appress

Tib*_*. M 6

使用Regexp构造函数代替

const ip_address = '217.138.216.62'

const re = new RegExp(`^${ip_address.replace(/\./g, '\\.')}$`, 'gi')

console.log(re.test('217.138.216.62'))

console.log(re.test('217.138.216.6'))
Run Code Online (Sandbox Code Playgroud)

在这里,我们使用边界类型断言^$)来匹配仅从头到尾找到完整模式的情况。