Jamex Jamex - 1 year ago 79
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);


Answer Source

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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download