escape a string so that it can be matched exactly in a regex constructor.

const
substring = "(++h.e.l.l.o++)",
my_regex = new RegExp(`${substring}\(world\)`),
my_str = "this string consist of (++h.e.l.l.o++)(world) positioned somewhere in the middle"
console.assert(my_regex.test(my_str) === true)
  • Parameters

    • str: string

    Returns string