We're planting a tree for every job application! Click here toΒ learn more

Regular Expressions Part 1

Ian Mugenya

30 May 2022

β€’

2 min read

Regular Expressions Part 1
  • JavaScript

REGEX BANNER.png

Regular expressions are a broad and complex topic. Most programmers usually shy away from even attempting to master them. Granted, the topic can look and seem daunting, but it's an essential topic nonetheless.

This is my attempt to understand them at a deeper level. I'll do this through a series of blog posts that I'm going to be writing over the next couple of weeks. This is part one, we will be describing regular expressions, and see how to write them in javascript. Here is a brief overview of the structure I'm going to use:

  • Part One

  • An introduction to Regular Expressions

  • What is a Regular Expression

  • How to Write a Regular Expression in Javascript

  • Part Two

  • Using Regular Expressions in Javascript

  • We'll take a deep dive into the test() and exec() methods and see how to utilize them to match strings

  • We'll look at matching multiple occurrences of a pattern as well as single occurrences

  • We'll explore the different flags available to us e.g g flag

  • We'll look at how to use Character Sets and Ranges

  • How to use Quantifiers

  • How to Capture Groups

  • Part Three

  • Build an Email validation system using regex.

Introduction

So what are regular expressions? Let's have a look at a few definitions:

A regular expression is a specific kind of text pattern that you can use with many modern applications and programming languages. -Regular Expressions Cookbook, Second Edition

A regular expression is a sequence of characters that specifies a search pattern in text. Usually, such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. -Wikipedia

Regular expressions are patterns used to match character combinations in strings. -MDN

Pay close attention to the definitions, and you will recognize that a few words keep popping up:

  • pattern
  • match
  • search
  • character

So with that in mind, we can make a few assumptions :

  1. Regular Expressions are patterns

  2. Regular Expressions contain characters or strings of characters

  3. You can use regular expressions to match, search or replace characters or strings of characters

    Now that we know what they are, how do we write them in code? Regular expressions are not unique to one programming language. There is an implementation of them in all programming languages. I'll be looking at how they are implemented in javascript. There are two main ways to define regular expressions in javascript:

Method One: Using Regular Expression Literal

let regex = /hello/;

In the first method, you create a pattern by passing in the literal pattern between the two slashes.

Method Two: Using the RegExp constructor function in javascript

let regex = new RegExp('hello');

When using the second method, you use the in-built RegExp constructor function to create the pattern that will match. The function takes in the literal pattern that will match, as an argument.

So far, we've covered what a regular expression is, and how to create it. The next logical step is to look at how to use them. How do you test whether a sentence or a word matches the pattern specified in the regex? That is what I'll be covering in my next blog post. Thanks for taking the time to read this and happy coding!!

Did you like this article?

Ian Mugenya

Family πŸ‘ͺ Code πŸ‘¨πŸΏβ€πŸ’» Dogs 🐢

See other articles by Ian

Related jobs

See all

Title

The company

title

Remote

Title

The company

title

Remote

Title

The company

title

Remote

Title

The company

title

Remote

Related articles

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

β€’

12 Sep 2021

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

β€’

12 Sep 2021

WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works
email iconhello@works-hub.comUK flag

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

US flag

108 E 16th Street, New York, NY 10003

Subscribe to our newsletter

Join over 111,000 others and get access to exclusive content, job opportunities and more!

Β© 2022 WorksHub

Privacy PolicyDeveloped by WorksHub