Day 12 Of #30DaysOfJavaScript

Day 12 Of #30DaysOfJavaScript

ยท

5 min read

Introduction

Hey folks, I hope that you must have read my Day 11 Article on #30DaysOfJs. In case, if you haven't then make sure you give it a read by clicking here.

Regular Expressions

A regular expression or RegExp is a small programming language that helps to find patterns in data. A RegExp can be used to check if some pattern exists in different data types. To use RegExp in JavaScript either we use the RegExp constructor or we can declare a RegExp pattern using two forward slashes followed by a flag. We can create a pattern in two ways.

To declare a string we use a single quote, double quote a backtick to declare a regular expression we use two forward slashes and an optional flag. The flag could be g, i, m, s, u or y.

  1. Parameters- A regular expression takes two parameters. One required search pattern and an optional flag.
  • Pattern: A pattern could be a text or any form of pattern with some sort of similarity. For instance, the word spam in an email could be a pattern we are interested to look for in an email or a phone number format.

  • Flags: are optional parameters in a regular expression that determine the type of searching. Let us see some of the flags:

    • g: a global flag which means looking for a pattern in the whole text

    • i: case insensitive flag(it searches for both lowercase and uppercase)

    • m: multiline

  1. Creating a Pattern With RegExp Constructor-

For Example 1:

// Without flag
let pattern = 'love'
let regEx = new RegExp(pattern)

For Example 2:

// With flag
let pattern = 'love'
let flag = 'gi'
let regEx = new RegExp(pattern, flag)

Declaring a regex pattern using RegExp object. Writing the pattern and the flag inside the RegExp constructor. For example-

let regEx = new RegExp('love','gi');
  1. Creating a Pattern Without RegExp Constructor-

For Example 1:

// With Flag
let regEx= /love/gi

For Example 2:

// Without Flag
let regEx= new RegExp('love','gi')
  1. RegExpp Object Methods-
  • test(): Tests for a match in a string. It returns true or false.

For Example:

const str = 'I love JavaScript'
const pattern = /love/
const result = pattern.test(str)
console.log(result)

// Output
// true
  • match(): Returns an array containing all of the matches, including capturing groups, or null if no match is found. If we do not use a global flag, match() returns an array containing the pattern, index, input and group.

For Example:

const str = 'I love JavaScript'
const pattern = /love/
const result = str.match(pattern)
console.log(result)

// Output
// ["love", index: 2, input: "I love JavaScript", groups: undefined]
  • search(): Tests for a match in a string. It returns the index of the match, or -1 if the search fails.

For Example:

const str = 'I love JavaScript'
const pattern = /love/g
const result = str.search(pattern)
console.log(result)

// Output --> 2
  1. Square Bracket-

For Example 1:

const pattern = '[Aa]pple' // this square bracket means either A or a
const txt = 'Apple and banana are fruits. An old cliche says an apple a day keeps the  doctor way has been replaced by a banana a day keeps the doctor far far away. '
const matches = txt.match(pattern)

console.log(matches)  

// Output
// ["Apple", index: 0, input: "Apple and banana are fruits. An old cliche says an apple a day keeps the  doctor way has been replaced by a banana a day keeps the doctor far far away.", groups: undefined]

For Example 2:

const pattern = /[Aa]pple/g // This square bracket means either A or a
const txt = 'Apple and banana are fruits. An old cliche says an apple a day a doctor way has been replaced by a banana a day keeps the doctor far far away. '
const matches = txt.match(pattern)

console.log(matches)  

// Output
// ["Apple", "apple"]
  1. Escape Character(\) in RegExp-

For Example:

const pattern = /\d/g  // d is a special character which means digits
const txt = 'This regular expression example was made in January 12,  2020.'
const matches = txt. match(pattern)

console.log(matches)  

// Output
// ["1", "2", "2", "0", "2", "0"], this is not what we want
  1. One or more times (+)-

For Example:

const pattern = /\d+/g  // d is a special character which means digits
const txt = 'This regular expression example was made in January 12,  2020.'
const matches = txt. match(pattern)
console.log(matches)  

// Output
// ["12", "2020"], this is not what we want
  1. Period(.)-

For Example:

const pattern = /[a].+/g  // . any character, + any character one or more times 
const txt = 'Apple and banana are fruits'
const matches = txt.match(pattern)

console.log(matches)  

// Output
// ['and banana are fruits']
  1. Zero or One Time(?)- Zero or one time. The pattern may not occur or it may occur once.

For Example:

const txt = 'I am not sure if there is a convention how to write the word e-mail.\
Some people write it email others may write it as Email or E-mail.'
const pattern = /[Ee]-?mail/g  // ? means optional
matches = txt.match(pattern)

console.log(matches)  

// Output
// ["e-mail", "email", "Email", "E-mail"]
  1. Exact Match- It should have ^ starting and $ which is an end.
let pattern = /^[A-Z][a-z]{3,12}$/;
let name = 'Asabeneh';
let result = pattern.test(name)

console.log(result) 

// Output
// true

I hope that you must have found this article quite helpful. If yes, then do give a read to some of my other articles!

Who knows you might become a great programmer ๐Ÿค”!

ย