Regular Expressions 101

Community Patterns

Email Validator in JavaScript w/explanation

1

Regular Expression
ECMAScript (JavaScript)

/
^(([^<>()[\]\\.,;:@"]+(\.[^<>()[\]\\.,;:@"]+)*)|(".+"))@((\[[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,}))$
/
gm

Description

  • ^ : Start of the line.
  • (([^<>()[\]\\.,;:@"]+(\.[^<>()[\]\\.,;:@"]+)*)|(".+")) : This is the local part of the email, before the @ symbol. It can be one of two formats:
    • [^<>()[\]\\.,;:@"]+(\.[^<>()[\]\\.,;:@"]+)* : This means one or more characters that are not special characters (like <, >, (, ), [, ], \, ,, ;, :, @, ") followed by a period . and more non-special characters. This pattern can repeat.
    • ".+" : This means any characters surrounded by quotes.
  • @ : This is the @ symbol in the email.
  • ((\[[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,})) : This is the domain part of the email, after the @ symbol. It can be one of two formats:
    • \[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\] : This is an IP address format, like [192.168.0.1].
    • ([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,} : This is a domain name format, like example.com. It means one or more alphanumeric characters or hyphens followed by a period ., and this pattern can repeat. It ends with two or more alphabetic characters.
  • $ : End of the line.
Submitted by CodeByAidan - 25 days ago (Last modified 25 days ago)