Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation in a regex

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

like image 641
aphexlog Avatar asked Feb 16 '26 15:02

aphexlog


1 Answers

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.

like image 75
Tibebes. M Avatar answered Feb 18 '26 03:02

Tibebes. M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!