JavaScript - What is RegExp Object?
Last Updated :
05 Dec, 2024
Improve
The RegExp object in JavaScript is a powerful tool for pattern matching, searching, and manipulating strings. It allows you to define patterns for matching text and helps in validation, extraction, and replacement.
1. String Searching
Check if a String Contains a Word
let s = "Welcome to JavaScript!";
let regex = /JavaScript/;
console.log(regex.test(s));
let s = "Welcome to JavaScript!";
let regex = /JavaScript/;
console.log(regex.test(s));
Output
true
Case-Insensitive Search
let s = "Learning javascript is fun!";
let regex = /JavaScript/i;
console.log(regex.test(s));
let s = "Learning javascript is fun!";
let regex = /JavaScript/i;
console.log(regex.test(s));
Output
true
2. String Validation
Validate an Email Address
let mail = "user@example.com";
let regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(regex.test(mail));
let mail = "user@example.com";
let regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(regex.test(mail));
Output
true
Validate a Password
let pass = "P@ssw0rd";
let regex = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
console.log(regex.test(pass));
let pass = "P@ssw0rd";
let regex = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
console.log(regex.test(pass));
Output
true
3. Pattern Matching and Extraction
Extract Phone Numbers
let s = "Call me at 123-456-7890 or 987-654-3210.";
let regex = /\d{3}-\d{3}-\d{4}/g;
console.log(s.match(regex));
let s = "Call me at 123-456-7890 or 987-654-3210.";
let regex = /\d{3}-\d{3}-\d{4}/g;
console.log(s.match(regex));
Output
[ '123-456-7890', '987-654-3210' ]
Extract Domain Names from Emails
let mails = "user1@example.com, user2@test.org";
let regex = /@(\w+\.\w+)/g;
let a = [];
let match;
while ((match = regex.exec(mails)) !== null) {
a.push(match[1]);
}
console.log(a);
let mails = "user1@example.com, user2@test.org";
let regex = /@(\w+\.\w+)/g;
let a = [];
let match;
while ((match = regex.exec(mails)) !== null) {
a.push(match[1]);
}
console.log(a);
Output
[ 'example.com', 'test.org' ]
4. Search and Replace
Replace All Occurrences of a Word
let s1 = "JavaScript is fun. JavaScript is powerful.";
let s2 = s1.replace(/JavaScript/g, "JS");
console.log(s2);
let s1 = "JavaScript is fun. JavaScript is powerful.";
let s2 = s1.replace(/JavaScript/g, "JS");
console.log(s2);
Output
JS is fun. JS is powerful.
Sanitize Input by Removing Special Characters
let s1 = "Hello@World#2024!";
let s2 = s1.replace(/[!@#]/g, "");
console.log(s2);
let s1 = "Hello@World#2024!";
let s2 = s1.replace(/[!@#]/g, "");
console.log(s2);
Output
HelloWorld2024
5. Dynamic Pattern Matching
Highlight User Input in Text
let s1 = "JavaScript is awesome!";
let s2 = "javascript";
let regex = new RegExp(s2, "i");
console.log(s1.replace(regex, "**JavaScript**"));
let s1 = "JavaScript is awesome!";
let s2 = "javascript";
let regex = new RegExp(s2, "i");
console.log(s1.replace(regex, "**JavaScript**"));
Output
**JavaScript** is awesome!
Built-In Methods of RegExp
- test(): Checks if a pattern exists in a string.
- exec(): Executes a search for a match and returns details about the match.
- match(): Used with strings to find matches.
- replace(): Replaces matched text with new content.
Real-World Use Cases
- Form Validation: Ensure user inputs match required formats, like emails, phone numbers, or passwords.
- Log Parsing: Extract data like IP addresses, error messages, or timestamps from server logs.
- Dynamic Search and Highlighting: Enable search functionality with user-defined patterns, like search filters in applications.
- Data Cleaning: Remove unwanted characters or transform text into standardized formats.
- Web Scraping: Extract meaningful data from raw HTML or web content for analysis.