Skip to content

Instantly share code, notes, and snippets.

@PrismaticDevs
Last active March 11, 2022 19:38
Show Gist options
  • Save PrismaticDevs/6a8d829e562f6eca6f88f1b99afa5b3e to your computer and use it in GitHub Desktop.
Save PrismaticDevs/6a8d829e562f6eca6f88f1b99afa5b3e to your computer and use it in GitHub Desktop.

Email Validation with Regex

The following regular expression validates a user's input to check that it has all the components of a valid email address.

^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$

Summary

This regex checks to see if the input before the "@" contains numbers or letters, has a period or hyphen to separate values, then checks for an "@" symbol,then checks for letters for the domain name after the "@", checks that there's a dot after the domain, and lastly checks for another set of letters with a minimum value of 2 and a maximum of 6 letters after the ".".

All instances of quotes, or " in this gist are to denote an exact aharacter match, such as "@" saying that the literal "at" symbol is being refernced.

Table of Contents

Regex Components

Anchors

  • "^" asserts position at start of the string,
  • "^" matches the start of a string without consuming any characters,
  • "$" matches the end of a string without consuming any characters,
  • "@" matches the literal character "@",
  • "." checks for the period character,
  • "{2, 6}" checks for a string between 2-6 letters

Quantifiers

  • The "^" quantifier tells you if there are exact character matches and groups of character matches of a specific size.
  • The "+" quantifier asserts a reition of similiar sharacters to the one immedietely before the "+".
  • Another example of a quantifier in this regex is the {2,6} which checks that there a minimum of 2 and a maximum of 6 characters after the period following the domain name.

Grouping Constructs

  • In this regex the parentheses surrounding characters denote a grouping construct.
  • The first group asserts that a series of characters may exist with a period or underscore in between them.
  • The second group checks for a series of alphabetic characters following the "@" symbol.
  • The final group uses a quantifier to check for between 2-6 alphabetical acharacters that collow the period, for example ".com" and ".org"

Bracket Expressions

  • In the only bracket expression in this regex is "{2,6}" and it defines that the length of the string after the period can be between 2 and 6 characters

Character Classes

  • In the square brackets in this regex define a specific group or type of characters to look for that are present, which in this regex are aplhanumeric characters, a period or underscore, and the "@" symbol.

Character Escapes

  • A backslash before a character defines a specific type of regex value rather than the character following it.
  • Example: "\d" Represents a digit rather than just the letter "d".

Author

Matthew Brignola PrismaticDevs

@PrismaticDevs
Copy link
Author

Character classes and bracket expressions are basically the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment