-3

I don't understand why rg = new RegExp(`/${a}.*${b}/`) doesn't work below whereas rg = /\(.*\)/ does work.

let a = '\(';
let b = '\)';
let rg;
//rg = new RegExp(`/${a}.*${b}/`); // doesn't work
rg = /\(.*\)/;
let match = rg.exec(`test (regex works) is ok`);
if (match) {
  console.log(match[0]); // -> (regex works)
}
Ivar
  • 5,377
  • 12
  • 50
  • 56
user310291
  • 35,095
  • 76
  • 252
  • 458

1 Answers1

-1

Because you would need to double escape the \ in the string.

let a = '\\(';
let b = '\\)';
epascarello
  • 195,511
  • 20
  • 184
  • 225