mostafa amine
mostafaamine

mostafaamine

How to Use Regular Expressions

How to Use Regular Expressions

How to write Regular Expressions

mostafa amine
·Feb 26, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • 1-First of all, let’s see what mean regular expressions

In this article, we will see the most important thing in JavaScript that you need to learn right now

Note that regular expressions are not only supported in javascript but in many languages including python

1-First of all, let’s see what mean regular expressions

  • regular expressions or regex are patterns that help you to match search and replace text
  • Regular expression are very powerful and very helpful
  • But when you start to learn regex can be hard because they use special characters

In this article, you will see a full guide to learn regular expression in the right way

If you want to find a word in a specific string ‘javascript is easy’ there are So many ways but the easy way is to use the following regular expression /javascript/

  • Javascript has multiple ways to use regex one of the ways is using .test() method

  • .test() method returns true or false

Let’s take a simple example :

let mystr = "just google it"
let myregex = /google/
console.log(myregex.test(mystr))
  • Output : true

Do you see its simple

Up until now, we see how to find a word in a specific string

  • But sometimes you want to ignore case (letter) or in other words, ignore the difference between uppercase letters and lowercase in this situation we have to use a global flag

  • The i flag ignores the difference between uppercase letters and lowercase

  • You can use it by appending it to the regex for example: /HelloWorld/i

  • Here you can match HellOwOrld and helloworld and HELLOWORLD

let mystr = "just GoOgLE it"
let myregex = /google/i
console.log(myregex.test(mystr))

.match() method

So far you have only been checking if a pattern exist or not but you can also extract the actual matches you found with the .match() method

  • to use .match() method correctly apply the method on a string and pass the regex inside the parentheses it’s the opposite of the .test() method
  • for example :
let mystr = "mostafaamine"
let regex = /mostafa/
console.log(mystr.match(regex))
  • Output : 'mostafa'

In the previous examples we see just how to find a specific word but what if you want to find a word more than once

  • Here we have to use another flag it’s the g flag

The g flag simply match a word more than one time for example :

  • Without using g flag :
let mystr = "mostafa mostafa mostafa"
let regex = /mostafa/
console.log(mystr.match(regex))
  • Output : 'mostafa'
  • when we use g flag :
let mystr = "mostafa mostafa mostafa"
let regex = /mostafa/g
console.log(mystr.match(regex))
  • Output : [‘mostafa’ , ‘mostafa’ , ‘mostafa’]

How about matching whitespaces

  • To match white space in javascript we can use .replace() methode with the following regular expression : /\s/g

  • \s with a lower case s

  • Character Classes :

/s: matches any whitespace characters such as space

/S : matches any non-whitespace characters

/d : matches any digit character

/D : matches any non-digit characters

/w: matches any word character

/W : matches any non-word character

For example :

let mystr = "mostafa mostafa mostafa"
let regex = /\s/g
console.log(mystr.replace(regex,""))
  • Output : mostafamostafamostafa

So far I have given you only some examples, not all of them, and regular expressions contain a lot of properties, so I will give you a course made by Freecodecamp. It is a comprehensive course that explains regular expressions in detail Regular Expressions

Did you find this article valuable?

Support mostafa amine by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this