Jamex Jamex - 7 months ago 27
Javascript Question

javascript syntax for regex using variable as pattern

I have a variable patt with a dynamic numerical value

var patt = "%"+number+":";

What is the regex syntax for using it in the test() method?

I have been using this format

var patt=/testing/g;
var found = patt.test(textinput);



Yeah, you pretty much had it. You just needed to pass your regex string into the RegExp constructor. You can then call its test() function.

var matcher = new RegExp("%" + number + ":", "g");
var found = matcher.test(textinput);

Hope that helps :)