Explain RegExp in ES6
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. | 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. |
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:
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:
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:
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:
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:
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:
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: