-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
  • 6,138
  • 12
  • 49
  • 61
user310291
  • 36,946
  • 82
  • 271
  • 487

1 Answers1

-1

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

let a = '\\(';
let b = '\\)';
epascarello
  • 204,599
  • 20
  • 195
  • 236