Regex 101
Tags: javascript, webdev, tutorial, codequality
Published: 2021-07-21 18:01:56

Hey readers 👋🏻, in this blog we are going to talk about `Regular Expressions` or we can also call it `REGEX` .

`REGEX` is sequence of characters which are in a certain patter, and these patterns help us `find` or `find and replace` or `validate` things like  `email, passwords and usernames`. 

## Let's start learning 🐱‍🏍:


&nbsp;


### **Basics :**

The most easy way to explain `regex` with an example is if:

we want to search the word `JavaScript` in a string.

![first](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm1pbxe8mptm9v1uj2z5.png)

Now this example is very basic but believe me `REGEX` has lots and lots of use-cases.

&nbsp;


### **Multiple Possible Characters :** 

Let's see an example where you want to see if the string contains many possibilities for example if you want to search for `dog` or `cat` .

We can do this by using  `| ` the `OR` sign.

![second](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdqipdsne87yr860ejim.png)

 Here if the `petString` would contain `Shreyas loves JavaScript` then the output would have been `false`.

**To be clear, the REGEX patterns are case sensitive, so if a string would contain `shreyas` and I search for `ShreyaS` then the output would be `false`.**

&nbsp;

### **Case Sensitiveness :** 

What should we do when we are not sure about the case... No worries! We can make our REGEX Pattern ignore the case.

![three](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c93agu0yjurjyxupkafn.png)

As you can see we have used `i` in `regex1`, and there are many such flags which gives us a lot of control over the pattern.

`i` stands for `irrespective of the case`.

Here we are using `.test()` method, which is an inbuilt method in JavaScript, which returns true or false according to the pattern entered.

The Syntax is `pattern.test(String-which-has-to-be-tested)` . 
&nbsp;
### **Global Searching :**

`.test()` has a draw back, which is that it only returns true or false, and if true it does not tell us how many times the pattern was matched, so to back this drawback, JS has another inbuilt method called as `.match()` which let's us know how many times the pattern is matched in the string.

`.match()` return an array of results which have successfully matched the pattern, and the array's length is the time the pattern was recognized.

Let me show an example 👇🏻:

![four](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1j1ti5rxlxxjh458ylaj.png)

Here you can see the syntax of `.match()` is a little bit different when compared to `.test()` 

`.match()` 's syntax is :  `string.match(regex-pattern);`

Also you can see that I have used another flag, which is `g` and it stands for `global`, which helps us find the perfect match globally in the string.

&nbsp;

### **Find Group of Letters :**

We can group many letters together to find them inside a string. REGEX gives us flexibility with `Character Classes`, these allow us to define a group of characters and they have to be enclosed in `[  ]` (Square Brackets) .

It will be more clear when you see an example.

`We have to find every vowel inside a string.` 👇🏻
![five](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rxcqgdgavjuu5cgqjr1.png)

The pattern has 2 flags, which are non case sensitive and to check globally in the string. 

Here `[aeiou]` vowels are grouped together and are individually searched in the string.

&nbsp;

### **Match anything using Wildcard  period / dot :**

Some times we just have to search for words which end with some certain letters or which start from some certain letters. To do so, we have wildcard period which is basically a `.` period/dot. 

If we have to match words which end with the letters `un` . For example `fun` or `run` or `sun` .

For that we have 👇🏻

![six](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfb5az0vfcc9u2z1ygkk.png)

This pattern will check for any word ending with un and it will do it irrespective of the case(`i` flag) and would search in the whole string (`g` flag).

&nbsp;

### **Range of Characters :**

We can also provide a range of characters to check from.

![seven](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cuap54wk9u9bf1jasg9.png)

 For Example: If you are sure that there are possibilities that the word may start with any character but the ending will be with the letters `at` , then we can give a range of characters which will check the string and if matched then return an array.

**Note:** *If no value is found, NULL would be returned!*

&nbsp;

### **Match Numbers :**

What if, you wanna match numbers?? Don't worry REGEX has got you covered!

Just like characters we can write `/[0-9]/g`, that's it, all the numbers are covered.

But as we all know, us developers...we are lazy 💤! So why to write `/[0-9]/` when you can also write `/[\d]/g` and this `d` stands for digits!

&nbsp;

### **Match Number and Characters :**

To match number and characters, we can write 👇🏻

![eight](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69f62zyfg14i9chdwnyy.png)

But isn't this REGEX Pattern too long?? We have a shorthand for this, which is `/\w/g` and instead of the whole REGEX pattern you can just write the shorthand.

&nbsp;
### **Check for Minimum and Maximum Characters   :**

We  can set a min and a max amount of characters.

![nine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/za8lmn75jgca1evfa5qx.png)

This REGEX pattern allows only those strings which have equal or greater length then 5 and are under or equal to 10.

The syntax for that is `/[regex here]{min-number, max-number}/g`.

------

## Challenge 🎯:

I wanna give a quick challenge to all the readers, why don't you make a REGEX which verifies usernames, and the conditions are:

1. Username should have numbers.
2. Username can have an underscore.
3. Username should not have any special characters. 
4. Username should have minimum 5 characters and maximum 15 characters. 

------

Thank you so much for reading the whole blog 🎉, if you liked it do share it with your friends and implement REGEX in your upcoming projects! It has saved me from writing lots of line of code and a lot of time too, I am sure it will be very effective for you too!

Till the next blog... Goodbye 👋🏻 !!