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:
Javascript
var regEx = /[neveropen]/gi; var chk_string = "Geeks for Geeks is " + "a learning platform for neveropen." ; 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. | 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:
Javascript
var regEx = new RegExp( '[neveropen]+' , 'gi' ); var chk_string = "Geeks for Geeks is" + " a learning platform for neveropen." ; var match1 = chk_string.match(regEx); console.log(match1); |
Output:
['Geeks', 'Geeks', 's', 'e', 'g', 'neveropen']
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:
Javascript
var regEx = new RegExp( '\s' ); var chk_string = "Geeks for Geeks is " + "a learning platform for neveropen." ; var match1 = chk_string.replace(regEx, '\t' ); console.log(match1); |
Output:
Geek for Geeks is a learning platform for neveropen.
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:
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.