Here is the video version for this topic. You can read the written version which comes after the video section.

Let's finally understand everything about regex. Regex is short for Regular Expression.

The purpose of this course is to help you understand regex. I'll be simplifying regular expressions in a couple of videos, using many examples to explain and show you different regular expressions you can write for different needs.

Regular Expressions can be interesting, powerful, but also frustrating to create or get correctly. In my programming journey, I've struggled with understanding regular expressions. And when I think I finally understand it, I see a problem where I need to use it, and I get confused on how to use it. And I know it's not just me.

So my goal for this course, is that you finally get a hang of it. You're able to read and understand regular expressions comfortably and also able to write complex regular expressions.

Can you read this?

/^(https?:\/\/)?(www\.)?([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,}(\/[^\s]*)?$/

Maybe you can't for now, but at the end of this course, we'll revisit this, and see if you understand it.

Regular Expressions are used for creating patterns which match different parts of a string. Let's take this string for example:

โ€œHello, I have a bag worth $50, a shoe worth $100 and a hat worth $90โ€

Let's say we want to search this string for the word โ€œbagโ€. It's easy right? We just need to search for a โ€œbโ€ followed by โ€œaโ€ followed by โ€œgโ€. If things were always this straightforward, we wouldn't need regular expressions. But things are not always this literal.

For example, what if we wanted to search this string for a dollar sign $, followed by a number 0-9?

You cannot simply search for a โ€œdollar signโ€ followed by โ€œ5โ€ followed by โ€œ0โ€, because even though that matches the โ€œ$50โ€, it does not match the โ€œ$100โ€. And it also doesn't match the โ€œ$90โ€.

So for you to be able to accurately search for the part of this long string which starts with a dollar sign, and is followed with a number, be it 5, 10, 100, 1000, you need to create a pattern.

Using a regular expression, you can define patterns like:

  • symbol followed by a number
  • letter preceded by a hyphen
  • a bunch of mixed characters in a particular order
  • and so many advanced patterns.

Regular expressions are not only for finding the parts of a string that matches a pattern. They are also used for validating that a string follows a pattern.

For example, in applications today, developers use regular expressions to validate that the string of an email address a user enters is valid. If the user provides something like deeecode111@gmail.com, the regular expression passes when tested with the string. If the user enters my-javascript@yahoo, the regular expression knows that this is invalid.

As you can see, email addresses are not straightforward. It can involve numbers, hyphens, and can be short or long. We can't do a search for this without regular expressions.

Developers also use regular expressions to validate that passwords meet a criteria. For example, you see in websites:

โ€œyour password must contain 1 letter, 1 number, and 1 symbol and must be 10 characters longโ€

You create a pattern for that, and test the password with the pattern, and you know if the password is valid or invalid.

There are so many other patterns you can create with Regular Expressions. It's not just the simple things like letter followed by numberโ€ฆno! As we progress in this course, you'd see the advanced patterns you can also write.

I also want to clarify that Regular Expressions are not only a feature in JavaScript. They are also supported in several other languages. And they are mostly similar. What I mean by similar is that a regular expression in one language, would most likely achieve the same result in another language.

But there can be slight differences which depends on the language or the compiler of the language.

Regular Expressions as I will explain in this course can be applied to not just JavaScript but to several other languages that support regular expressions.

This course is a playlist on YouTube . Here's the outline for the videos under the course:

For the first section of videos, we'll look at

  1. Terms used in Regex
  2. Your first Regex
  3. Flags
  4. Regex Engines

For the second section, we'll look at how to create different patterns which involves:

  1. Character Classes
  2. Quantifiers
  3. Meta Characters
  4. Special Characters
  5. Capturing Groups
  6. Alternation
  7. Lookarounds

Then we,

  1. Wrap up

One more thing I want you to understand is there can be different patterns for something you want to match. With regular expressions, there's no only one way. There can be thousands of ways. For example, my regex pattern for validating an email address may be different from yours.

One way might be more readable than another or shorter than other.