How I can passing quantifier as variable in this regexp str.match(/^(\-)?\d+( (\-)?\d+){3}$/)

Recommended Answers

All 4 Replies

You can if you use something like this:

var q = "3";
var re = new RegExp("/^(\-)?\d+( (\-)?\d+){" + q + "}$");
if (re.test(str))
    //...

I try this:

var q = "3";
var re = new RegExp("/^(\-)?\d+( (\-)?\d+){" + q + "}$");
alert(re.test("1 2 3 4"));

but return false

still false

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.