0

This question on SO led me to this website which have a bunch of explanation on email validation. The last regex seems the most accurate and recommended. I've searched SO for this regex but the implementations I found slightly differ.

I would like to test the following regex provided on the website with my current set of emails and make some tests.

I tried to make the title and this question as precise as possible (for you and the search engines), feel free to suggest edits.

The regex in question is the following:

\A(?=[a-z0-9@.!#$%&'*+/=?^_`{|}~-]{6,254}\z)
 (?=[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@)
 [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
@ (?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+
  (?=[a-z0-9-]{1,63}\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z

I escaped the / characters but it still doesn't seem to work for some reason.

My goal is to use this regex like so:

const regexEmail = new RegExp("...")

I created a sandbox for this regex available on regex101, but the email address is not found.

I am no pro at regex and usually get only pre made stuff. The help of this community is greatly appreciated.

HypeWolf
  • 750
  • 12
  • 29

3 Answers3

1

try with this

^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$
Nuthan Kumar
  • 483
  • 5
  • 22
0

You need to construct your RegExp on a single line, since your actual regular expression contains unescaped backticks (which means you can't use a multi-line string).

const regexEmail = new RegExp("\A(?=[a-z0-9@.!#$%&'*+/=?^_`{|}~-]{6,254}\z)(?=[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z");

console.log(regexEmail);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You need to remove all \A and \z from your regex, which denote the start and end of a string in Python, yet equate to the literal characters A and z in JavaScript.

Python JavaScript

Removing these shows the regex working as expected on Regex101 and in the following:

const email = "ragedog@hotmail.com";
const regex = new RegExp("(?=[a-z0-9@.!#$%&'*+\/=?^_`{|}~-]{6,254})(?=[a-z0-9.!#$%&'*+\/=?^_`{|}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63})[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", "gm");

console.log(regex.test(email));
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • `\z` does not mean the end of string in Python. It is `\Z` in Python that matches the end of string. – Wiktor Stribiżew Feb 28 '19 at 00:01
  • Oh I see, I'm very glad you stopped by. I didn't see we could switch between languages on the side. I also thought Regex expressions were universal. – HypeWolf Feb 28 '19 at 00:29