-1

I am trying to match words like lay_10, lay_11, lay_20 with regex but it's not working, any help will be really appreciated.

var patt =new RegExp("/lay/");

if (patt.test("lay_10")){
  alert("matched");
}
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30

3 Answers3

0

Rewrite your code like the following:

var patt = new RegExp("lay");

if (patt.test("lay_10")) {
  alert("matched");
}
skwidbreth
  • 7,888
  • 11
  • 58
  • 105
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
0
const reg = /lay_([0-9]+)/g

if(reg.test(`lay_10`)) {
    console.log(`Matched`)
}

Hope this helps.

0

To clarify the other answers, there are two ways to define a regex in JavaScript. Either via new RegExp("lay"); or via /lay/. Since you mixed the two methods, it did not work correctly.

var pattern1 = new RegExp("lay");
var pattern2 = new /lay/           //You can use either one.

if (pattern1.test("lay_10") && pattern2.test("lay_11")) {
  alert("matched"); // matched
}
Bronzdragon
  • 345
  • 3
  • 13