JavaScript RegExp(Regular Expression)
Last Updated :
20 Nov, 2023
A regular expression is a sequence of characters that forms a search pattern. Regular expressions can be used to perform all types of text search and text replacement operations
A regular expression can be a single character or a more complicated pattern.
Syntax:
/pattern/modifiers;
Example:
let patt = /GeeksforGeeks/i;
Explanation :
/GeeksforGeeks/i is a regular expression.
GeeksforGeeks is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching:
g |
Find the character globally |
i |
Find a character with case-insensitive matching |
m |
Find multiline matching |
Regular Expression Brackets can Find characters in a specified range
[abc] |
Find any of the characters inside the brackets |
[^abc] |
Find any character, not inside the brackets |
[0-9] |
Find any of the digits between the brackets 0 to 9 |
[^0-9] |
Find any digit not in between the brackets |
(x | y) |
Find any of the alternatives between x or y separated with | |
Regular Expression Metacharacters are characters with a special meaning:
\. |
Search single characters, except line terminator or newline. |
\w |
Find the word character i.e. characters from a to z, A to Z, 0 to 9 |
\d |
Find a digit |
\D |
Search non-digit characters i.e all the characters except digits |
\s |
Find a whitespace character |
\S |
Find the non-whitespace characters. |
\b |
Find a match at the beginning or at the end of a word |
\B |
Find a match that is not present at the beginning or end of a word. |
\0 |
Find the NULL character. |
\n |
Find the newline character. |
\f |
Find the form feed character |
\r |
Find the carriage return character |
\t |
Find the tab character |
\v |
Find the vertical tab character |
\uxxxx |
Find the Unicode character specified by the hexadecimal number xxxxx |
Regular Expression Quantifiers are used to define quantitiesoccurrence
n+ |
Match any string that contains at least one n |
n* |
Match any string that contains zero or more occurrences of n |
n? |
Match any string that contains zero or one occurrence of n |
m{X} |
Find the match of any string that contains a sequence of m, X times |
m{X, Y} |
Find the match of any string that contains a sequence of m, X to Y times |
m{X,} |
Find the match of any string that contains a sequence of m, at least X times |
m$ |
Find the match of any string which contains m at the end of it |
^m |
Find the match of any string which contains m at the beginning of it |
?!m |
Find the match of any string which is not followed by a specific string m. |
Regular Expression Object Properties:
constructor |
Return the function that created the RegExp object’s prototype |
global |
Specify whether the “g” modifier is set or not |
ignorecase |
Specify whether the “i” modifier is set or not |
lastindex |
Specify the index at which to start the next match |
multiline |
Specify whether the “m” modifier is set or not |
source |
Return the text of RegExp pattern |
Regular Expression Object Methods:
compile() |
Used to compile the regular expression while executing of script |
exec() |
Used to test for the match in a string. |
test() |
Used to test for a match in a string |
toString() |
Return the string value of the regular expression |
Below is an example of the JavaScript Regular Expressions.
Example:
JAVASCRIPT
function GFGFun() {
let str = "Visit geeksforGeeks" ;
let n = str.search(/GeeksforGeeks/i);
console.log(n);
}
GFGFun();
|
Output:
6
Using String Methods
In JavaScript, regular expressions are often used with the two string methods: search() and replace().
- The search() method uses an expression to search for a match and returns the position of the match.
- The replace() method returns a modified string where the pattern is replaced.
Using String search() With a Regular Expression
Use a regular expression to do a case-insensitive search for “GeeksforGeeks” in a string:
Example:
JAVASCRIPT
function myFunction() {
let str = "Visit geeksforGeeks!" ;
let n = str.search(/GeeksforGeeks/i);
console.log(n);
let n = str.search(/GeeksforGeeks/);
console.log(n);
}
myFunction();
|
Output:
6
-1
Use String replace() With a Regular Expression
Use a case-insensitive regular expression to replace gfG with GeeksforGeeks in a string:
Example:
JAVASCRIPT
function myFunction() {
let str = "Please visit gfG!" ;
let txt = str.replace(/gfg/i, "geeksforgeeks" );
console.log(txt);
}
myFunction();
|
Output:
Please visit geeksforgeeks!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...