Regular Expressions 101

Community Patterns

Email regex validation

13

Regular Expression
ECMAScript (JavaScript)

/
^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$
/
gm

Description

RegEx email

/^((?!\.)[\w-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/gim;

Just playing with Reg Ex. This to validate emails in following ways

  • The email couldn't start or finish with a dot
  • The email shouldn't contain spaces into the string
  • The email shouldn't contain special chars (<:, *,ecc)
  • The email could contain dots in the middle of mail address before the @
  • The email could contain a double doman ( '.de.org' or similar rarity)

Groups

There was created 3 groups into this validations that could be used for custom purposes or replacements

mailname@domain.com

  • First group takes the first string with the name of email $1 => (mailname)
  • Second group takes the @ plus the domain: $2 => (@domain)
  • Third group takes the last part after the domain : $3 => (.com)
Submitted by https://www.linkedin.com/in/peralta-steve-atileon/ - 5 years ago (Last modified 8 months ago)