-1

I assume it has something to do with using a variable, when I test it online I don't use a variable and it works. I haven't used much regex so I could just be doing something silly too.

let home = 'testhome';
let currentHome = 'testhome (8)';
let re = new RegExp(home + ' \(\d+\)');

if (currentHome.match(re)) {
    //no match
} else {
    // this is the code executed
}

Any help appreciated. There could be multiple numbers in the brackets of currentHome.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
user3238415
  • 168
  • 1
  • 10

1 Answers1

0

When using RegExp constructor you should escape the escape character itself in order to form a correct reg expression.

let re = new RegExp(home + ' \\(\\d+\\)');

Karen Grigoryan
  • 4,829
  • 2
  • 18
  • 34