Skip to content

Instantly share code, notes, and snippets.

@JaberSE09
Last active June 18, 2022 16:57
Show Gist options
  • Save JaberSE09/4565db1ae5a4d184a391289ab8ae1408 to your computer and use it in GitHub Desktop.
Save JaberSE09/4565db1ae5a4d184a391289ab8ae1408 to your computer and use it in GitHub Desktop.
Email Regular Expression

Matching an Email

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

This is a regular expression which means a expression that is used to match a certain format. This is used to validate an email.

Summary

The use of the regular expression is used to validate the email and provide a email format to be used. Using regular expressions is a way to limit errors from users and get what is expected from the input

Table of Contents

Regex Components

Any regular Expression all or most of these characteristics. Through the breakdown of the email regular expression it will demonstrate the different parts to a regular expression.

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

Anchors

The beginning of the regular expression we see the (^) sign which beings the regular expression. Then the second one which is ($) which is the end of the regular expression through these make up the anchors.

Quantifiers

Then we have the (+) where it is grouping the email name , email address like yahoo or gmail and the .com. Also the {2,6} which is used to identify the character from then [a-z.].

Character Classes

It allows you to match any character from a character set. The \d which is used to match any single digit is a character class used in the email validator.

Grouping and Capturing

The groups are [a-z0-9_.-]+) which is for the email name and the second one is ([\da-z.-]+) for email label like gmail or yahoo. There is also a group for ([a-z.]{2,6}) to get the .com notation

Bracket Expressions

Email validation includes the character sets of [a-z0-9_.-], which is for any letter a-z and is case sensitive. It also has a character (0-9) and the characters (_) , (-) , and (.) and has [\da-z.-] which is gores through a single digit from (0-9), any character a-z which is case sensitive, and the characters (.) and (-) [a-z.] goes through any character (a-z) case sensitive and the character (.).

Greedy and Lazy Match

The expression is greedy match because it has the (+) and the {2,6} elements in the expression. These give the greedy (+) matches many times and the {2,6} matches for the last group

Author

https://github.com/JaberSE09

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