Regular Expression In JavaScript
Regex or Regular expression is an arrangement of characters used to match the character combinations in a given string.
OR
In other words, Regular expression is a search pattern that can search for a particular pattern of characters in a given character sequence. One of the major applications of Regex is form validation. For example, if we want to test that the input in a field is a valid email id or not then we can test the input against an appropriate Regex.
Creating the Regular Expression
Regular expressions are also objects. There are two ways to create a regular expression,
Regular Expression Literals
var v='/pattern/modifier'; // i is the modifier
We can enclose the pattern between slashes.
RegExp Constructor
RegExp Constructor
We can also use the constructor function of RegExp object.
var exp=RegExp('pattern');
We should use the constructor function when the expression can change during the execution. A regular expression can contain a single character or a complicated pattern with multiple characters. Regular expressions can be used to perform all types of text search and text to replace operations.
Using search() and replace() functions
The search() method returns the index position of the match if successful, otherwise, it returns -1;
The replace() method replaces some character sequences in the string with some input character sequence.
//Search and replace with
normal string inputs
str='Java,
Python and C/C++ programming';
pos=str.search('Python');
document.write(pos);
str='Java,
Python and C/C++ programming';
pos=str.replace('Python','JavaScript');
document.write(pos);
Output:
6
Java, JavaScript and C/C++ programming
We can also search and replace with RegExp,
//Search and replace with
RegExp inputs
str='Java, Python and C/C++ programming';
pos=str.search(/python/i); //i is the modifier
document.write(pos); //for NOT case-sensitive
document.write('<br>'); //Matching
str='Java,
Python and C/C++ programming';
pos=str.replace(/python/i,'JavaScript');
document.write(pos);
Output:
6
Java, JavaScript and C/C++ programming
Modifiers
The Modifiers have the role to affect the way in which search will be performed,Modifier |
Role |
---|---|
i | Used for case-insensitive matching |
g | Used for a global match (find all matches rather than stopping after the first match) |
m | Used for multiline matching |
Expressions and Brackets
To search specific characters in a range we can use brackets(brackets are used to specify the inclusion or exclusion of boundary alphabets or numbers).Expressions |
Role |
---|---|
[abc] | searches for any of the characters within the brackets |
[0-9] | searches for any of the digits in range 0 to 9(0 and 9 inclusive) |
(x | y) | searches for anyone x or y separated with | |
Quantifiers
There are some quantifiers used for identifying the number of occurrences of an alphabet or digit in the character sequence.Quantifier |
Role |
---|---|
n+ | Used to match any string that contains at least one n |
n* | Used to match any string that contains zero or more occurrences of n |
n? | Used to matches any string that contains zero or one occurrence of n |
Methods Used By Regular Expressions
There are many methods that can use regular expressions,Method |
Role |
---|---|
exec() | This method executes a search for a match in a string. It returns an array of information or null if not matched. |
test() | This method tests for a match in a string. It returns true or false. |
match() | This method returns an array containing all of the matches, including capturing groups, or null on a mismatch. |
matchAll() | This method returns an iterator containing all of the matches. |
search() | This method tests whether a string is matched or not and returns the index of the match, or -1 otherwise. |
replace() | This method executes a search for a match in a string and replaces the matched substring with a replacement character sequence. |
replace() | This method uses a regular expression or a fixed string as the input to split down a string into an array of substrings. |
Examples