Regular Expressions 101

Community Library Entry

0

Regular Expression
ECMAScript (JavaScript)

/
^(?=^[\w.@!#$%&'*+/=?^`{|}~-]{7,254}$)(?<username>(?=^[\w.!#$%&'*+/=?^`{|}~-]{1,64}@)[\w!#$%&'*+/=?^`{|}~-]+(?:\.[\w!#$%&'*+/=?^`{|}~-]+)*)@(?<domain>(?=\b[a-z\d.-]{5,252}$)(?:[a-z\d](?:[a-z\d-]{0,61}[a-z\d])\.)+(?:(?:[a-z](?:[a-z-]{0,61}[a-z]))|(?:(?=[a-z\d-]*?[a-z])(?=[a-z\d-]*?[\d])[a-z\d](?:[a-z\d-]{0,61}[a-z\d]))))$
/
gi

Description

Regex to validate an email input with the following rules...

Whole address:

  • Up to 254 characters.
  • Case insensitive.
  • Username and domain separated by @ symbol.
  • Only one @ symbol allowed.
  • No spaces allowed.

Username:

  • 1 to 64 characters.
  • Any letter, digit and symbol from these #!%$'&+*-/=?^_.{|}~ are allowed.
  • It cannot start or end with a dot.
  • It cannot have two (or more) consecutive dots.

Domain:

  • Only letters, digits and hyphens (and dots of course) allowed.
  • It cannot start or end with a dot.
  • Unlimited number of labels separated by one dot allowed.
  • No label can start nor end with hyphens.
  • It cannot have two (or more) consecutive dots.
  • 2 to 63 characters per label.
  • It cannot have an all-numerical TLD.
Submitted by chCharly - 2 years ago (Last modified 2 years ago)