Q:

Why doesn't this particular regex work in JavaScript?

I have this regex on Javascript :
var myString="aaa@aaa.com";
var mailValidator = new RegExp("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (!mailValidator.test(myString))
{
    alert("incorrect");
}

but it shouldn't alert "incorrect" with aaa@aaa.com. 
It should return "incorrect" for aaaaaa.com instead (as example).
Where am I wrong?

A:

It works if you do this:
var mailValidator = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

What happens in yours is that you need to double escape the backslash because they're inside a string, like "\\w+([-+.]\\w+)*...etc
Here's a link that explains it (in the "How to Use The JavaScript RegExp Object" section).

