JavaScript RegExp (Regular Expression)
Last Updated :
03 Dec, 2024
A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It’s often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).
In JavaScript, RegExp is an object that is used to create regular expressions, which are patterns used to match character combinations in strings.
JavaScript
// 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.
JavaScript
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).
JavaScript
let regex = /hello/i;
let s = "Hello world";
console.log(regex.test(s));
- In this example, 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
JavaScript
let regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
console.log(regex.test("[email protected]"));
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.
JavaScript
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.
JavaScript
let regex = /foo/g;
let res = "foo bar foo".replace(regex, "baz");
console.log(res);
Similar Reads
Markdown Cheat Sheet
Markdown is a simple way to write and format text using plain text symbols. It's a lightweight language that lets you create nicely formatted text without the use of complex coding. The idea is to make it easy for people to read and understand the formatting, even if they're looking at the raw text.
6 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is
13 min read
JavaScript Cheat Sheet - A Basic Guide to JavaScript
JavaScript is a lightweight, open, and cross-platform programming language. It is omnipresent in modern development and is used by programmers across the world to create dynamic and interactive web content like applications and browsers JavaScript (JS) is a versatile, high-level programming language
15+ min read
Git Cheat Sheet
Git Cheat Sheet is a comprehensive quick guide for learning Git concepts, from very basic to advanced levels. By this Git Cheat Sheet, our aim is to provide a handy reference tool for both beginners and experienced developers/DevOps engineers. This Git Cheat Sheet not only makes it easier for newcom
10 min read
React Cheat Sheet
React is an open-source JavaScript library used to create user interfaces in a declarative and efficient way. It is a component-based front-end library responsible only for the view layer of a Model View Controller (MVC) architecture. React is used to create modular user interfaces and promotes the
10 min read
JavaScript RegExp (Regular Expression)
A regular expression is a special sequence of characters that defines a search pattern, typically used for pattern matching within text. It's often used for tasks such as validating email addresses, phone numbers, or checking if a string contains certain patterns (like dates, specific words, etc.).
4 min read
Bootstrap Cheat Sheet - A Basic Guide to Bootstrap
Bootstrap is a free, open-source, potent CSS framework and toolkit used to create modern and responsive websites and web applications. It is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites. Nowadays, websites are perfect for all browsers and all
15+ min read
jQuery Cheat Sheet â A Basic Guide to jQuery
What is jQuery?jQuery is an open-source, feature-rich JavaScript library, designed to simplify the HTML document traversal and manipulation, event handling, animation, and Ajax with an easy-to-use API that supports the multiple browsers. It makes the easy interaction between the HTML & CSS docum
15+ min read
Angular Cheat Sheet - A Basic Guide to Angular
Angular is a client-side TypeScript-based, front-end web framework developed by the Angular Team at Google, that is mainly used to develop scalable single-page web applications(SPAs) for mobile & desktop. Angular is a great, reusable UI (User Interface) library for developers that helps in build
15+ min read