Open In App

Explain RegExp in ES6

Improve
Improve
Like Article
Like
Save
Share
Report

A RegEx or RegExp is used to denote regular expression. A Regular Expression is an object that describes a pattern of characters. It allows us to search for specific patterns of the text using defined patterns.

Why use RegExp?

  • RegEx is mainly used for form validation on web pages.
  • To check whether a given string is valid or not.
  • To check that the entered username should only contain alphabets and numbers.
  • To check whether the entered email contains valid syntax or not.
  • To check password syntax if it is according to the given instructions or not.

There are two ways to define a Regular Expression in ES6:

Literal notation:

The pattern is enclosed between the two forward slashes. It is executed at compile time.

Syntax:

let regExp = /pattern/;

Constructor function:

The pattern is given inside double quotes in RegExp() (regular expression) class function. It gets memory dynamically. It is executed at runtime.

Syntax:

let regExp = new RegExp( pattern, parameters );

Sets and Ranges: They are represented by several characters inside square brackets […]. It is used to search for any character among given values or ranges inside square brackets.

S. No. Patterns Description
1. [pattern] It checks for any one character from the pattern.
2. [^pattern]  It checks for any one character not from the pattern.

Example 1: In this JavaScript example, a regular expression, [geeks]/gi, is defined to match any occurrence of the characters ‘g’, ‘e’, ‘e’, ‘k’, or ‘s’ in a case-insensitive manner. The test string, formed by concatenating two sentences, is then analyzed using the match method with the defined regular expression. The result, stored in the match1 variable, is an array containing all the matches found in the string.

Javascript




let regEx = /[geeks]/gi;
let chk_string = "Geeks for Geeks is "
+ "a learning platform for geeks.";
let match1 = chk_string.match(regEx);
console.log(match1);


Output

[
  'G', 'e', 'e', 'k', 's',
  'G', 'e', 'e', 'k', 's',
  's', 'e', 'g', 'g', 'e',
  'e', 'k', 's'
]

Quantifiers: Quantifiers specify the position and frequency of a character in a pattern.

S. No. Quantifiers Description Example
1. + Matches strings with at least one or more characters in a given range.  /([a – z]+)/
2. * Matches strings with zero or more characters in a given range. /([A – Z]*)/
3. {m} Matches strings with ‘m’ number of characters. /([0 – 9]{2})/
4. {m1, m2} Matches strings with characters in range from m1 to m2 /([a –  z]{1, 4})/
5. {m, } Matches strings with a minimum ‘m’ number of characters. /(pattern{3, })/
6. $ Matches strings ending with a given value or character. /gfg$/
7. ^ Matches strings starting with a given value or character. /^gfg/

Example 2: In this JavaScript code, a regular expression is created using the RegExp constructor with the pattern [geeks]+ and flags 'gi'. This regular expression aims to match one or more occurrences of the characters ‘g’, ‘e’, ‘e’, ‘k’, or ‘s’ in a case-insensitive manner. The test string, formed by concatenating two sentences, is then subjected to the match method with the constructed regular expression.

Javascript




let regEx = new RegExp('[geeks]+', 'gi');
let chk_string = "Geeks for Geeks is"
+ " a learning platform for geeks.";
let match1 = chk_string.match(regEx);
console.log(match1);


Output

[ 'Geeks', 'Geeks', 's', 'e', 'g', 'geeks' ]

Literal characters: These literals are used to denote the escape characters.

S. No. Literal characters Description
1. \0 It specifies the NULL character in the string.
2. \t It specifies the tab space in the string.
3. \v It specifies the vertical tab in the string.
4. \n It specifies the new line character in the string.
5. \r It specifies the carriage return in the string.

Example 3: In this JavaScript code, a regular expression is defined using the RegExp constructor with the pattern '\s', aiming to match any whitespace character. The test string, formed by concatenating two sentences, undergoes the replace method with the constructed regular expression. The replacement value '\t' is used to substitute each matched whitespace character with a tab.

Javascript




let regEx = new RegExp('\s');
let chk_string = "Geeks for Geeks is "
+ "a learning platform for geeks.";
let match1 = chk_string.replace(regEx, '\t');
console.log(match1);


Output

Geek     for Geeks is a learning platform for geeks.

Meta characters: These characters are used to specify the type of characters in a string.

S. No. Metacharacters Description Example
1. \s It is used to specify the blank space in the string. /welcome to\s gfg/
2. \S It is used to specify the non-blank space in the string /welcome\S to gfg/
3. \w It is used to specify the word character in the string. /welcome\w to gfg/
4. \W It is used to specify the non-word character in the string. /welcome to\W gfg/
5. \d It is used to specify the decimal digit character in the string. /welcome to\d gfg/
6. \D It is used to specify the non-decimal digit character in the string. /welcome to gfg\D/

Example 4: In this JavaScript code, a regular expression (\d+)\s(\d+)\s(\d+) is defined to capture three groups of consecutive digits separated by spaces. The test string, “0 1 2 3 4 5 6 7 8 9,” is logged to the console before any modifications. The replace method is then applied to the chk_string using the defined regular expression, replacing the matched digit groups with the string “Geeks for Geeks.”

Javascript




let regEx = /(\d+)\s(\d+)\s(\d+)/;
let chk_string = "0 1 2 3 4 5 6 7 8 9";
console.log("Before:");
console.log(chk_string);
let match1 = chk_string.replace(regEx, 'Geeks for Geeks');
console.log("After:");
console.log(match1);


Output

Before:
0 1 2 3 4 5 6 7 8 9
After:
Geeks for Geeks 3 4 5 6 7 8 9

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  



Last Updated : 17 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads