Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Explain RegExp in ES6

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.PatternsDescription
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:

Javascript




var regEx = /[geeks]/gi;
var chk_string = "Geeks for Geeks is "
+ "a learning platform for geeks.";
var 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.QuantifiersDescriptionExample
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:

Javascript




var regEx = new RegExp('[geeks]+', 'gi');
var chk_string = "Geeks for Geeks is"
+ " a learning platform for geeks.";
var 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 charactersDescription
1.\0It specifies the NULL character in the string.
2.\tIt specifies the tab space in the string.
3.\vIt specifies the vertical tab in the string.
4.\nIt specifies the new line character in the string.
5.\rIt specifies the carriage return in the string.

Example 3:

Javascript




var regEx = new RegExp('\s');
var chk_string = "Geeks for Geeks is "
+ "a learning platform for geeks.";
var 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.MetacharactersDescriptionExample
1.\sIt is used to specify the blank space in the string./welcome to\s gfg/
2.\SIt is used to specify the non-blank space in the string/welcome\S to gfg/
3.\wIt is used to specify the word character in the string./welcome\w to gfg/
4.\WIt is used to specify the non-word character in the string./welcome to\W gfg/
5.\dIt is used to specify the decimal digit character in the string./welcome to\d gfg/
6.\DIt is used to specify the non-decimal digit character in the string./welcome to gfg\D/

Example 4:

Javascript




var regEx = /(\d+)\s(\d+)\s(\d+)/;
var chk_string = "0 1 2 3 4 5 6 7 8 9";
console.log("Before:");
console.log(chk_string);
var 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.  


My Personal Notes arrow_drop_up
Last Updated : 06 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials