javascript - RegExp Object Literal notation vs Constructor notation difference -
this question has answer here:
var digits = "b2"; var re = new regexp("/\d+/"); console.log(/\d+/.test(digits)); console.log("digits matches :", digits.match(re));
why digits.match(re) return null whereas .test returns true? when run digits.match("/\d+/"); correct answer.
the new regexp()
syntax doesn't use forward slashes /
delimiter. it's assumed entire string regex. also, backslashes must escaped, since requirement of strings when aren't referencing escape character.
these same:
digits.match(new regexp("\\d+")); // 2 digits.match(/\d+/); // 2