Using regex with a variable to search a sentence

I am using the following regex to seach a sentence and want to match only values that are followed by a period. I am confused to why the following regex is true since the ta isnt followed by a period. How would I change it so doesnt match?

let text = 'Paracetamol takes up to an hour to work to '
let x = "ta"

let regex = new RegExp(x + ".", "i")

A period in a regular expression has a special meaning. It will match any single character (except line terminators). If you want to match the period character then you need to escape it.

I know how escape the period using regular regex but it doesn’t work when I use the regex constructor

i have tried

let regex = new RegExp(x + "\.", "i")

but it doesn’t work

Nevermind I forgot I needed to use two backslashes when using the regex constructor

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.