Question

I am using the following regex to validate an email:

^[a-zA-Z0-9!$'*+/\-_#%?^`&=~}{|]+(\.[a-zA-Z0-9!$'*+/\-_#%?^`&=~}{|]+)*@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-['&_\]]]+)(\.[\w-['&_\]]]+)*))(\]?)$

This works fine in C# but in JavaScript, its not working.... and yes I replaced every backslash with a double backslash as the following:

^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-['&_\\]]]+)(\\.[\\w-['&_\\]]]+)*))(\\]?)$

I am using XRegExp. Am I missing something here? Is there such thing as a converter to convert normal regex to JavaScript perhaps :) ?

Here is my function:

 function CheckEmailAddress(email) {
     var reg = new XRegExp("^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-['&_\\]]]+)(\\.[\\w-['&_\\]]]+)*))(\\]?)$")

     if (reg.test(email) == false) {
         return false;
     }

     return true;
 }

It is returning false for a simple "abc@123.com" email address.

Thanks in advance!

Kevin

Was it helpful?

Solution

The problem is that your regular expression contains character class subtractions. JavaScript's RegExp does not support them, nor does XRegExp. (I initially misremembered and commented that it does, but it does not.)

However, character class subtractions can be replaced with negative lookaheads so this:

[\w-['&_\\]]]

can become this:

(?:(?!['&_\\]])\\w)

Both mean "any word character but not one in the set '&_]". The expression \w does not match ', & or ] so we can simplify to:

(?:(?!_)\\w)

Or since \w is [A-Za-z0-9_], we can just remove the underscore from the list and further simplify to:

[A-Za-z0-9]

So the final RegExp is this:

new RegExp("^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([A-Za-z0-9]+)(\\.[A-Za-z0-9]+)*))(\\]?)$")

I've done modest testing with this RegExp, but you should do due diligence on checking it.

It is not strictly necessary to go through the negative lookahead step to simplify the regular expression but knowing that character class subtractions can be replaced with negative lookaheads is useful in more general cases where manual simplification would be difficult or brittle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top