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);
TIA
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 :)
\s, you will need to double-escape some special characters, Eg: new RegExp(`\\s*${var}`, 'g') to match all whitespace up to a variable.You have to build the regex using a regex object, rather than the regex literal.
From your question, I'm not exactly sure what your matching criteria is, but if you want to match the number along with the '%' and ':' markers, you'd do something like the following:
var matcher = new RegExp("%" + num_to_match + ":", "g");
You can read up more here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
You're on the right track
var testVar = '%3:';
var num = 3;
var patt = '%' + num + ':';
var result = patt.match(testVar);
alert(result);
Example: http://jsfiddle.net/jasongennaro/ygfQ8/2/
You should not use number. Although it is not a reserved word, it is one of the predefined class/object names.
And your pattern is fine without turning it into a regex literal.
.test() method. And number is not a reserved word.test() for match(). Thanks for the catch. Also, you're right about number not being a reserved word. Edited that too to be more clear. Typing to quickly without double-checking my facts. ;-)