Dynamically assigning a Regex pattern in Javascript -
how dynamically assign pattern?
i've tried:
var str = "hebbbbllo"; var patt = new regexp; patt =("b", "g"); console.log(str.match(patt).length);
but code produces error:
uncaught typeerror: cannot read property 'length' of null
i've tried use pattern "/b/g"
doesn't work
you should this:
var patt = new regexp("pattern","flags");
so, this:
var str = "hebbbbllo"; var patt = new regexp("b","g"); console.log(str.match(patt).length);
read more @ mdn
update:
if want in way, can do:
var str = "hebbbbllo"; var patt = regexp; var regex = patt("b", "g"); console.log(str.match(regex).length);
Comments
Post a Comment