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 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo