How to Use Regular Expressions in JavaScript (Practical Guide with Examples)
Complete JavaScript regex guide with real examples. test(), match(), replace(), exec(). Common patterns for email, phone, and URLs.
Regex Basics in JavaScript
JavaScript has built-in regular expression support. Regex literals are written between forward slashes: /pattern/flags.
Two ways to create a regex:
``
// Literal syntax (preferred when pattern is known)
const regex = /hello/i;
// Constructor syntax (for dynamic patterns)
const pattern = 'hello';
const regex = new RegExp(pattern, 'i');
`
Core JavaScript Regex Methods
test() — Returns true/false
`
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/;
emailRegex.test('user@example.com'); // true
emailRegex.test('not-an-email'); // false
`
match() — Returns matches array
`
const text = 'Prices: ₹500, ₹1200, ₹350';
const prices = text.match(/₹d+/g); // ['₹500', '₹1200', '₹350']
`
With the g flag, returns all matches. Without g, returns first match with groups.
replace() — Find and replace
`
// Remove all HTML tags
const clean = html.replace(/<[^>]+>/g, '');
// Format Indian phone numbers
const formatted = '9876543210'.replace(/(d{5})(d{5})/, '$1-$2');
// Result: '98765-43210'
`
replaceAll() — Replace all matches (ES2021+)
`
const text = 'foo bar foo';
text.replaceAll('foo', 'baz'); // 'baz bar baz'
`
split() — Split by pattern
`
// Split on any whitespace
'hello world tabs'.split(/s+/); // ['hello', 'world', 'tabs']
`
exec() — Detailed match with groups
`
const dateRegex = /(d{2})/(d{2})/(d{4})/;
const result = dateRegex.exec('Date: 15/03/2026');
// result[0]: '15/03/2026'
// result[1]: '15' (day)
// result[2]: '03' (month)
// result[3]: '2026' (year)
`
Regex Flags in JavaScript
| Flag | Meaning |
|---|---|
| i | Case insensitive |
| g | Global (find all matches) |
| m | Multiline (^ and $ match line boundaries) |
| s | Dotall (. matches newline) |
| u | Unicode mode |
`
/hello/gi.test('Hello World'); // true (case insensitive + global)
`
Named Capture Groups (ES2018+)
`
const dateRegex = /(?
const { day, month, year } = '15/03/2026'.match(dateRegex).groups;
// day: '15', month: '03', year: '2026'
`
Practical Patterns for Indian Development
Indian mobile number validation:
`
const isMobile = (num) => /^[6-9]d{9}$/.test(num.replace(/s|-/g, ''));
`
PAN card validation:
`
const isPAN = (pan) => /^[A-Z]{5}[0-9]{4}[A-Z]$/.test(pan.toUpperCase());
`
GST number validation:
`
const isGST = (gst) => /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z][1-9A-Z]Z[0-9A-Z]$/.test(gst);
`
Aadhaar number (basic format check):
`
const isAadhaar = (num) => /^[2-9]{1}[0-9]{11}$/.test(num.replace(/s/g, ''));
`
PIN code validation:
`
const isPincode = (pin) => /^[1-9][0-9]{5}$/.test(pin);
`
Email validation (practical, covers 99.9% of real cases):
`
const isEmail = (email) => /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/.test(email);
`
URL extraction from text:
`
const urls = text.match(/https?://[^s<>"{}|\^[]]+/g) || [];
``
Common Mistakes with JavaScript Regex
Forgetting to escape special characters in RegExp constructor:
`
// Wrong - dot matches any character
new RegExp('hello.world');
// Correct - escaped dot matches literal dot
new RegExp('hello\.world');
`
Forgetting the g flag for global replacement:
`
'aaa'.replace(/a/, 'b'); // 'baa' — only replaces first!
'aaa'.replace(/a/g, 'b'); // 'bbb' — replaces all
`
Testing regex against undefined:
`
// Safe pattern
const value = userInput ?? '';
if (/^d+$/.test(String(value))) { ... }
``
Testing Your Regex with Lazyblink
Before putting regex into production code, test it at lazyblink.com/tools/developer/regex-tester. Paste your pattern and test string to see matches highlighted in real time.
Frequently asked questions
How do I use regex in JavaScript?
Use regex literals (/pattern/flags) with methods like test() for boolean matching, match() for extracting matches, replace() for substitution, and split() for dividing strings.
How do I validate an Indian phone number with regex?
Use /^[6-9]\d{9}$/ after removing spaces and hyphens. This matches 10-digit numbers starting with 6, 7, 8, or 9.
What is the difference between test() and match() in JavaScript regex?
test() returns a boolean (true/false) — use it for validation. match() returns the matched text and capture groups — use it when you need to extract what was found.
Put this guide into practice with our free online tool — no signup required.
Open tool