Skip to content
Related Articles

Related Articles

Explain RegExp in ES6

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 27 Jan, 2022

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 given instruction 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.
3.[A-Z] It checks for any one character from the given range of uppercase alphabets.
4.[a-z] It checks for any one character from the given range of lowercase alphabets.
5.[0-9] It checks for any one character from the given range of digits.

Example 1:

HTML




<script>
    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);
</script>

Output:

Sets and Ranges

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:

HTML




<script>
        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);
</script>

Output:

Quantifiers

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:

HTML




<script>
    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);
</script>

Output:

Literal Character

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:

HTML




<script>
    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);
</script>

Output:

Meta Character


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!