-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
  • 607,720
  • 39
  • 448
  • 563
user3238415
  • 188
  • 1
  • 11

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
  • 5,234
  • 2
  • 21
  • 35