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:
Use of Brackets: It is used to mention a particular range of characters in a pattern.
Patterns with brackets | Description |
---|---|
[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.
Quantifiers | Description |
---|---|
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. |
^pattern | Matches strings with pattern as the beginning sequence. |
Use of literal characters: It is used to specify the escape characters.
Literal Characters | Description |
---|---|
\0 | It denotes a NULL character. |
\t | It denotes a tab space. |
\n | It denotes a newline. |
\r | It denotes a carriage return. |
Use of metacharacters: It is used to specify only the type of characters.
Meta Characters | Description |
---|---|
\s | It denotes a blank or whitespace. |
\S | It denotes a non-blank or no space character. |
\d | It denotes a decimal digit character. |
\D | It denotes a non-digit character. |
\w | It denotes a word character (Any character sequence). |
\W | It 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.