Code Monkey home page Code Monkey logo

regex-tutorial's Introduction

Regex Tutorial For Matching an Email

Regular Expressions, or Regex, are helpful tools used across many programming languages to find a specified combination of characters. Regex allows the programmer to search for any variation of a string matching a pattern instead of being limited to a specific, or "literal", query. For example, in a large database with multiple email address entries, a Regex pattern search of [any name]@[any domain].[any extension] will easily find all email addresses.

Contents

Usage

GIVEN a regex tutorial

  • WHEN I open the tutorial
    • THEN I see a descriptive title and introductory paragraph explaining the purpose of the tutorial, a summary describing the regex featured in the tutorial, a table of contents linking to different sections that break down each component of the regex and explain what it does, and a section about the author with a link to the author’s GitHub profile
  • WHEN I click on the links in the table of contents
    • THEN I am taken to the corresponding sections of the tutorial
  • WHEN I read through each section of the tutorial
    • THEN I find a detailed explanation of what a specific component of the regex does
  • WHEN I reach the end of the tutorial
    • THEN I find a section about the author and a link to the author’s GitHub profile

Summary

This tutorial explains the different components in the following regex:

Matching an Email

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

Two ways to define a regular expression

let regexp = new RegExp("pattern", "flags");

let regexp = /pattern/; // no flags
let regexp = /pattern/gmi; // with flags g,m and i
  • Slashes /.../ tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings
  • ^ is an anchor that matches from the start of input -a-z matches lowercase alpha characters
  • 0-9 matches the numeric characters
  • _ matches the literal '_' character
  • . matches the literal '.' character
  • - matches the literal '-' character
  • @ matches the special character @
  • \. matches the literal .
  • $ is an anchor that matches to the end of the string

Note

A dot . on its own represents any character. In order to search for a dot within a string we need to 'escape' search by preponing the search for a dot with the backslash character. By using the \ it will make the following character a literal instead of a special character

Explanation for [a-z0-9_\.-] 

- matches all lowercase characters from a to z
- matches all numeric characters from 0 to 9
- matches special characters such as `_`, `-`, and `.`
Explanation for [\da-z\.-]

- `\d` matches for any decimal digit
- matches all lowercase characters from a to z
- matches special characters such as `.` and `-`
Explanation for [a-z\.]

- matches all lowercase characters from a to z
- matches special character, `.`

Credits

  • Created by Amir Hackett

regex-tutorial's People

Contributors

amir-hackett avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.