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");
}
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");
}
Rewrite your code like the following:
var patt = new RegExp("lay");
if (patt.test("lay_10")) {
alert("matched");
}
const reg = /lay_([0-9]+)/g
if(reg.test(`lay_10`)) {
console.log(`Matched`)
}
Hope this helps.
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
}