JavaScript RegExp (Regular Expressions) are patterns used to match, search, and manipulate text in strings. They are widely used for validation, parsing, and text processing tasks in JavaScript.
- Used with methods like test(), match(), and replace() to work with strings.
- Helps validate inputs such as emails, phone numbers, and passwords.
- Supports flags like g (global), i (case-insensitive), and m (multiline).
In JavaScript, RegExp is an object that is used to create regular expressions, which are patterns used to match character combinations in strings.
// The /i flag makes the pattern case-insensitive
const patt = /Geeks/i;
const s1 = "geeksforgeeks";
const s2 = "forgeeks";
console.log(patt.test(s1));
console.log(patt.test(s2));
Creating a RegExp in JavaScript
There are two primary ways to create a RegExp in JavaScript
1. Using the RegExp Constructor
The RegExp constructor is useful when you need to create a regular expression dynamically, such as when the pattern or flags might change based on user input or other conditions.
let pattern = "hello"; // Pattern to match
let flags = "i"; // Case-insensitive flag
let regex = new RegExp(pattern, flags);
let s = "Hello world";
console.log(regex.test(s));
- Here, we dynamically create a RegExp object using the RegExp() constructor.
- The pattern "hello" will match the string "Hello" because of the 'i' flag, which makes the search case-insensitive.
- regex.test(s) returns true because "Hello" matches the pattern "hello" in a case-insensitive manner.
2. Using Regular Expression Literal
This is the most common and simple way to create a regular expression in JavaScript. It’s especially useful when the pattern is static (doesn’t change dynamically).
let regex = /hello/i;
let s = "Hello world";
console.log(regex.test(s));
- Here, we define the regular expression directly using literal notation.
- /hello/i means we're looking for the word "hello", and the 'i' flag ensures the search is case-insensitive.
- regex.test(s) returns true because "Hello" matches the pattern "hello" case-insensitively.
Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching:
| Expressions | Descriptions |
|---|---|
| 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
| Expressions | Description |
|---|---|
| [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:
| Metacharacter | Description |
|---|---|
| \. | 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
| Quantifier | Description |
|---|---|
| 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:
| Property | Description |
|---|---|
| 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:
| Method | Description |
|---|---|
| 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 |
Use Cases of Regular Expressions
1. Validating Email Addresses
Regular expressions are frequently used for validating emails. Here’s an example that matches most email formats
let regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
console.log(regex.test("user@example.com"));
2. Extracting Specific Data from a String
You can use regular expressions to extract specific data from a string, like extracting all numbers from a text.
let regex = /\d+/g;
let res = "There are 123 apples and 456 oranges".match(regex);
console.log(res);
3. Replacing Substrings
Regular expressions are often used in replacing certain substrings in text.
let regex = /foo/g;
let res = "foo bar foo".replace(regex, "baz");
console.log(res);