Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ES6 | RegEx

  • Last Updated : 27 Feb, 2020

A RegEx is the short form of the Regular Expression, which is the sequence of characters that define a pattern. The characters in a RegEx can be alphabets, numbers or special characters. Usually, RegEx is used to find a pattern in a string to replace, delete or modify them. In ES6, a RegEx can be defined by 2 different notations.

  • Literal notation: The pattern is enclosed between slashes. It is executed in compile time. So, constant patterns can be given in literal notation.
     var regex = /pattern/
  • Constructor function: The pattern is given inside single quotes. It is executed in runtime. So, flexible patterns can be given in constructor function.
    var regex = new RegExp( pattern, optional arguments)

There are different ways of mentioning patterns which are listed below:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Use of Brackets: It is used to mention a particular range of characters in a pattern.

Patterns with bracketsDescription
[pattern]Any one character from the pattern.
[^pattern]Any one character not from the pattern.
[0-9]Any one decimal number between 0-9.
[a-z]Any one character from the lower case alphabets.
[A-Z]Any one character from the upper case alphabets.

Use of Quantifiers: It is used to specify the frequency and position of characters in a pattern.



QuantifiersDescription
pattern+Matches strings with atleast one or more pattern.
pattern*Matches strings with zero or more patterns.
pattern{n}Matches strings with ‘n’ number of patterns.
pattern{n1, n2}Matches strings with patterns in range from n1 to n2 (Both inclusive).
pattern{n, }Matches strings with minimum ‘n’ number of patterns.
pattern$Matches strings with pattern as the end sequence.
^patternMatches strings with pattern as the beginning sequence.

Use of literal characters: It is used to specify the escape characters.

Literal CharactersDescription
\0It denotes a NULL character.
\tIt denotes a tab space.
\nIt denotes a newline.
\rIt denotes a carriage return.

Use of metacharacters: It is used to specify only the type of characters.

Meta CharactersDescription
\sIt denotes a blank or whitespace.
\SIt denotes a non-blank or no space character.
\dIt denotes a decimal digit character.
\DIt denotes a non-digit character.
\wIt denotes a word character (Any character sequence).
\WIt denotes a non-word character.

Example:




<script>
    var text1 = "Welcome to GeeksforGeeks ";
  
    var text2 = "The numbers are - 0, 1, 2, "
              + "3, 4, 5, 6, 7, 8, 9. The "
              + "special characters are - @,"
              + " #, $, %, ^, &, *";
  
    // RegExp.match() and RegExp.replace()
    // have been used in the following
    // examples
  
    // Bracket notations
    // 1
    var regexp = new RegExp('[geeks]');
    console.log(text1.match(regexp));
      
    // 2
    var regexp = new RegExp('[^geeks]');
    console.log(text1.match(regexp));
      
    // 3
    var regexp = new RegExp('[0-9]');
    console.log(text2.match(regexp));
  
    // Quantifiers
    // 4
    var regexp = new RegExp('[geeks]*');
    console.log(text1.match(regexp));
      
    // 5
    var regexp = new RegExp('s{2}');
    console.log(text1.match(regexp));
  
    // Literal characters and meta characters
    // 6
    var regexp1 = /(\w+)\s(\w+)\s(\w+)/
    let newtext = text1.replace(regexp1, '$3 $2 $1');
    console.log(newtext);
  
    // 7
    var regexp1 = /(\d+)/;
    let newtext1 = text2.replace(regexp1, 'number');
    console.log(newtext1);
  
    // 8
    var regexp2 = /(\s+)/;
    console.log(text2.replace(regexp2, '\t'));
</script>

Output:

Note: The RexExp doesn’t have methods and properties on its own. But it inherits some functions and properties from the prototype.




My Personal Notes arrow_drop_up
Recommended Articles
Page :