Skip to content

Instantly share code, notes, and snippets.

@ra8200
Created August 8, 2021 15:41
Show Gist options
  • Save ra8200/dfacb0b82a6bfdd2cbbd029458a90e02 to your computer and use it in GitHub Desktop.
Save ra8200/dfacb0b82a6bfdd2cbbd029458a90e02 to your computer and use it in GitHub Desktop.
RegEx Tutorial
In this Gist I will give a tutorial of RegEx
@ra8200
Copy link
Author

ra8200 commented Aug 11, 2021

Regex Tutorial

In this tutorial I am going to explain the use of regex to match emails using the expression. Regex stands for regular expression. they are used as a sequence keyboard characters, that specify a type of search pattern. In this tutorial, I will be using this regex /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/. This expression can be used to validate emails using applications, websites and other technology like MongoDB.

Summary

A regex (regular expression) is a sequence of characters that represents a specific search pattern. They are usually used to find a particular pattern, replace characters or even validate specific characters within a string. When used in JavaScript they are used as objects. In this tutorial I will walk you through the components of a regex and how it's used in validating an email.

Table of Contents

Regex Components

Anchors

Anchors, which are the opener and closer of the regex, are used in the email expression to match are ^ , the beginning and $ which indicates the ending of the string.

Quantifiers

Quantifiers in this regex include + , used as an operator, that will connect the username to the email extension then to then domain (username@email.com). Another quantifier for this regex includes {2,6}, which will make sure the domain matches a range of 2-6 characters for the character set of [a-z\.].

Grouping and Capturing

Group and Capturing is when a group of the #1 in this expression is ([a-z0-9_\.-]+) that matches the user email name. The second capturing group is ([\da-z\.-]+) which will match the email service. Then lastly, capture group #3 is ([a-z\.]{2,6}) to capture the domain.

Bracketed Expressions

Bracketed Expressions are the character sets within the expression that hold the above captured groups. in the character set used above, [a-z0-9_\.-], states that the username, of the email can include a combination of letters, numbers and the special characters ". -_". They are held in the actual [].

Author

check out my github at https://github.com/ra8200 .

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