I need to add interpolation in a regex.
what I have right now is var re = /212.129.52.45/gi
what i need is to replace the 212.129.52.45 with ip_appress
use the Regexp constructor instead
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'))
Here we are using Boundary-type Assertions (^ and $) to match when only the full pattern is found from start-to-end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With