-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,307
  • 3
  • 12
  • 27

3 Answers3

0

Rewrite your code like the following:

var patt = new RegExp("lay");

if (patt.test("lay_10")) {
  alert("matched");
}
skwidbreth
  • 6,964
  • 9
  • 50
  • 97
Omid Nikrah
  • 2,307
  • 3
  • 12
  • 27
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
  • 340
  • 3
  • 13