Munsif.
AboutExperienceProjectsAchievementsBlogsContact
HomeAboutExperienceProjectsAchievementsBlogsContact
Munsif.

Frontend Developer crafting scalable web applications with modern technologies and clean code practices.

Quick Links

  • About
  • Experience
  • Projects
  • Achievements

Connect

© 2026 Shaik Munsif. All rights reserved.

Built with Next.js & Tailwind

0%
Welcome back!Continue where you left off
Back to Blogs
JavaScript

JavaScript Regular Expressions Mastery: Real-World Patterns & Validation

Master regex with real-world patterns — email validation, URL parsing, log extraction, password enforcement, and more. Covers groups, lookaheads, Unicode, and performance tips.

Feb 16, 202625 min read
JavaScriptRegExValidationInterview PrepBest Practices
JavaScript MasteryPart 11 of 11
  • 1. JavaScript Type Coercion Mastery: A Senior Engineer's Guide to the Chaos
  • 2. JavaScript Runtime Deep Dive: Event Loop, Call Stack & Async Execution
  • 3. Mastering JavaScript Callbacks: From Sync to Async & The Event Loop
  • 4. JavaScript Closures Deep Dive: Mastering Lexical Scope & Memory
  • 5. JavaScript Object Prototyping Mastery: A Visual Guide to Inheritance
  • 6. Demystifying the JavaScript 'this' Keyword: A Visual Guide
  • 7. JavaScript Promises & Promise Chaining Mastery: From Callbacks to Async Flow
  • 8. JavaScript Array Methods Mastery: Complete Guide from map() to reduce()
  • 9. JavaScript Map & Set Mastery: Complete Guide to Modern Data Structures
  • 10. JavaScript String Methods Mastery: Complete Guide from slice() to replaceAll()
  • 11. JavaScript Regular Expressions Mastery: Real-World Patterns & Validation

Introduction

Regular expressions (regex) are one of the most powerful tools in a developer's arsenal—and one of the most misunderstood. They let you search, validate, extract, and transform text with surgical precision. Yet many developers avoid them, copy-pasting patterns from Stack Overflow without truly understanding them.

This guide takes a different approach. Instead of dry theory, every pattern is tied to a real-world use case you'll actually encounter—email validation, URL parsing, log file analysis, password strength checking, and more. By the end, you'll read and write regex with confidence.

ℹ️ note

[!NOTE] JavaScript regex uses the RegExp object. You can create patterns using literal syntax /pattern/flags or the constructor new RegExp('pattern', 'flags').


The Regex Toolkit: Methods at a Glance

MethodReturnsUse Case
str.match(regex)Array of matches or nullFind all occurrences
str.matchAll(regex)Iterator of detailed matchesFind with capture groups
str.search(regex)Index or -1Find position of first match
str.replace(regex, rep)New stringReplace matches
str.replaceAll(regex, rep)New stringReplace all matches
str.split(regex)ArraySplit by pattern
regex.test(str)booleanTest if pattern matches
regex.exec(str)Match array or nullDetailed single match

1. Regex Fundamentals

1.1 Creating Regular Expressions

javascript
// Literal syntax (preferred for static patterns)
const regex1 = /hello/i;

// Constructor syntax (for dynamic patterns)
const searchTerm = 'hello';
const regex2 = new RegExp(searchTerm, 'i');

// When to use constructor: pattern is built from variables
function findWord(text, word) {
    const regex = new RegExp(`\\b${word}\\b`, 'gi');
    return text.match(regex);
}

findWord('Hello world, hello there!', 'hello');
// ['Hello', 'hello']
⚠️ warning

[!WARNING] When using new RegExp(), you must double-escape special characters: \\d instead of \d, \\b instead of \b.

1.2 Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveA matches a
mMultiline^ and $ match line boundaries
sDotAll. matches newlines too
uUnicodeCorrect handling of Unicode characters
dHas indicesProvides start/end indices for captures
vUnicodeSetsExtended Unicode property support (ES2024)
javascript
// Combining flags
const regex = /pattern/gim;

2. Character Classes & Quantifiers

2.1 Built-in Character Classes

PatternMeaningEquivalentReal-World Use
\dAny digit[0-9]Phone numbers, ZIP codes
\DNon-digit[^0-9]Strip numbers from text
\wWord character[a-zA-Z0-9_]Usernames, identifiers
\WNon-word character[^a-zA-Z0-9_]Find special characters
\sWhitespace[ \t\n\r\f\v]Trim, normalize spaces
\SNon-whitespace[^ \t\n\r\f\v]Extract tokens
.Any character (except newline)Wildcard matching
\bWord boundaryWhole word search

2.2 Quantifiers

QuantifierMeaningExampleMatches
*0 or more\d*'', '5', '123'
+1 or more\d+'5', '123' (not '')
?0 or 1colou?r'color', 'colour'
{n}Exactly n\d{4}'2026'
{n,}n or more\d{2,}'12', '123', '1234'
{n,m}Between n and m\d{2,4}'12', '123', '1234'

2.3 Greedy vs Lazy

javascript
const html = '<div>Hello</div><div>World</div>';

// Greedy (default) — matches as MUCH as possible
html.match(/<div>.*<\/div>/);
// ['<div>Hello</div><div>World</div>'] — grabs everything!

// Lazy (add ?) — matches as LITTLE as possible
html.match(/<div>.*?<\/div>/g);
// ['<div>Hello</div>', '<div>World</div>'] — each tag separately
💜 important

[!IMPORTANT] Use lazy quantifiers (*?, +?, ??) when parsing structured text like HTML, JSON, or log files to avoid over-matching.


3. Real-World Validation Patterns

3.1 Email Validation

javascript
function isValidEmail(email) {
    // RFC 5322 simplified — covers 99% of real emails
    const regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    return regex.test(email);
}

console.log(isValidEmail('user@example.com'));       // true
console.log(isValidEmail('john.doe+tag@gmail.com')); // true
console.log(isValidEmail('invalid@'));               // false
console.log(isValidEmail('@nodomain.com'));           // false
console.log(isValidEmail('spaces here@mail.com'));    // false

Breaking it down:

PartMeaning
^[a-zA-Z0-9.!#$%&'*+/=?^_{}~-]+`
@Literal @ symbol
[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?Domain label: starts and ends with alphanumeric
(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$Additional domain labels separated by dots

3.2 Password Strength Validation

javascript
function validatePassword(password) {
    const checks = {
        length:    password.length >= 8,
        uppercase: /[A-Z]/.test(password),
        lowercase: /[a-z]/.test(password),
        digit:     /\d/.test(password),
        special:   /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password),
    };

    const strength = Object.values(checks).filter(Boolean).length;

    return {
        checks,
        strength,
        label: strength <= 2 ? 'Weak' : strength <= 4 ? 'Medium' : 'Strong'
    };
}

console.log(validatePassword('hello'));
// { checks: { length: false, ... }, strength: 1, label: 'Weak' }

console.log(validatePassword('MyP@ssw0rd!'));
// { checks: { length: true, ... }, strength: 5, label: 'Strong' }

3.3 Phone Number Validation (International)

javascript
function validatePhone(phone) {
    // Supports: +1-234-567-8900, (234) 567-8900, 234.567.8900, +91 98765 43210
    const regex = /^(\+?\d{1,3}[-.\s]?)?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/;
    return regex.test(phone.trim());
}

console.log(validatePhone('+1-234-567-8900'));   // true
console.log(validatePhone('(234) 567-8900'));    // true
console.log(validatePhone('+91 98765 43210'));   // true
console.log(validatePhone('234.567.8900'));      // true
console.log(validatePhone('abc'));               // false

3.4 URL Validation & Parsing

javascript
function parseURL(url) {
    const regex = /^(https?):\/\/([^\/\s:]+)(?::(\d+))?(\/[^\s?#]*)?(?:\?([^\s#]*))?(?:#(\S*))?$/;
    const match = url.match(regex);

    if (!match) return null;

    return {
        protocol: match[1],
        hostname: match[2],
        port:     match[3] || null,
        pathname: match[4] || '/',
        query:    match[5] || null,
        hash:     match[6] || null
    };
}

console.log(parseURL('https://api.example.com:8080/users?page=1&limit=10#section'));
// {
//   protocol: 'https',
//   hostname: 'api.example.com',
//   port: '8080',
//   pathname: '/users',
//   query: 'page=1&limit=10',
//   hash: 'section'
// }

3.5 Credit Card Number Validation

javascript
function detectCardType(number) {
    const cleaned = number.replace(/\s|-/g, '');

    const patterns = {
        visa:       /^4\d{12}(?:\d{3})?$/,
        mastercard: /^5[1-5]\d{14}$/,
        amex:       /^3[47]\d{13}$/,
        discover:   /^6(?:011|5\d{2})\d{12}$/,
    };

    for (const [type, regex] of Object.entries(patterns)) {
        if (regex.test(cleaned)) return type;
    }
    return 'unknown';
}

console.log(detectCardType('4532 0151 1283 0366')); // 'visa'
console.log(detectCardType('5425 2334 3010 9903')); // 'mastercard'
console.log(detectCardType('3782 822463 10005'));    // 'amex'

4. Groups and Captures

4.1 Capturing Groups ()

javascript
// Extract date parts from ISO format
const dateStr = '2026-02-12T15:30:00Z';
const match = dateStr.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);

const [full, year, month, day, hour, minute, second] = match;
console.log({ year, month, day, hour, minute, second });
// { year: '2026', month: '02', day: '12', hour: '15', minute: '30', second: '00' }

4.2 Named Groups (?<name>)

Named groups make patterns self-documenting:

javascript
// Parse a git commit line
const commitLine = 'abc1234 John Doe <john@example.com> 2026-02-12 Fix login bug';

const regex = /^(?<hash>[a-f0-9]+)\s+(?<author>[^<]+)<(?<email>[^>]+)>\s+(?<date>\d{4}-\d{2}-\d{2})\s+(?<message>.+)$/;
const { groups } = commitLine.match(regex);

console.log(groups);
// {
//   hash: 'abc1234',
//   author: 'John Doe ',
//   email: 'john@example.com',
//   date: '2026-02-12',
//   message: 'Fix login bug'
// }

4.3 Non-Capturing Groups (?:)

When you need grouping but don't want to capture:

javascript
// Match file extensions without capturing the dot group
const filename = 'report.pdf';

// ❌ Captures unnecessary group
const match1 = filename.match(/\.(\w+)$/);
console.log(match1[1]); // 'pdf' (but index 0 is '.pdf')

// ✅ Using non-capturing group for alternatives
const isImage = /\.(?:jpg|jpeg|png|gif|webp|svg)$/i.test('photo.JPG');
console.log(isImage); // true

4.4 Backreferences

Reference previously captured groups within the same pattern:

javascript
// Find repeated words (common typo)
const text = 'The the quick brown fox fox jumped';

const regex = /\b(\w+)\s+\1\b/gi;
const matches = text.match(regex);
console.log(matches); // ['The the', 'fox fox']

// Fix duplicate words
const fixed = text.replace(/\b(\w+)\s+\1\b/gi, '$1');
console.log(fixed); // 'The quick brown fox jumped'
javascript
// Validate matching HTML tags
function hasMatchingTags(html) {
    return /<(\w+)[^>]*>.*?<\/\1>/s.test(html);
}

console.log(hasMatchingTags('<div>Hello</div>'));   // true
console.log(hasMatchingTags('<div>Hello</span>'));  // false

5. Lookahead & Lookbehind (Zero-Width Assertions)

These assert that a pattern exists (or doesn't) without consuming characters.

AssertionSyntaxMeaning
Positive Lookahead(?=...)Must be followed by
Negative Lookahead(?!...)Must NOT be followed by
Positive Lookbehind(?<=...)Must be preceded by
Negative Lookbehind(?<!...)Must NOT be preceded by

5.1 Password Enforcement with Lookaheads

javascript
// Password must have: 8+ chars, uppercase, lowercase, digit, special
function isStrongPassword(password) {
    const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/;
    return regex.test(password);
}

console.log(isStrongPassword('MyP@ss1'));     // false (too short)
console.log(isStrongPassword('MyP@ssw0rd'));  // true
console.log(isStrongPassword('mypassword1')); // false (no uppercase, no special)

5.2 Format Currency Without Capturing

javascript
// Add commas to numbers: 1234567 → 1,234,567
function formatNumber(num) {
    return num.toString().replace(
        /\B(?=(\d{3})+(?!\d))/g,
        ','
    );
}

console.log(formatNumber(1234567));    // '1,234,567'
console.log(formatNumber(1000000000)); // '1,000,000,000'
console.log(formatNumber(42));         // '42'

How it works:

  • \B — non-word boundary (not at start)
  • (?=(\d{3})+(?!\d)) — lookahead: followed by groups of 3 digits, then no more digits

5.3 Extract Prices Without Currency Symbol

javascript
// Get price values from text, excluding the $ symbol
const text = 'Laptop: $999.99, Mouse: $29.50, Keyboard: $79.00';

const prices = [...text.matchAll(/(?<=\$)\d+(?:\.\d{2})?/g)]
    .map(m => parseFloat(m[0]));

console.log(prices); // [999.99, 29.5, 79]

5.4 Find Words NOT Preceded by a Pattern

javascript
// Find "script" that's NOT part of "JavaScript"
const text = 'JavaScript uses script tags. TypeScript also has scripts.';

const matches = [...text.matchAll(/(?<!Java|Type)script/gi)]
    .map(m => ({ match: m[0], index: m.index }));

console.log(matches);
// [{ match: 'script', index: 21 }] — only the standalone "script"

6. Real-World Data Extraction

6.1 Parse Log Files

javascript
const logLine = '2026-02-12 15:30:42 [ERROR] UserService: Failed to authenticate user_id=12345 ip=192.168.1.100';

const logRegex = /^(?<timestamp>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s\[(?<level>\w+)\]\s(?<service>\w+):\s(?<message>.+)$/;
const { groups } = logLine.match(logRegex);

console.log(groups);
// {
//   timestamp: '2026-02-12 15:30:42',
//   level: 'ERROR',
//   service: 'UserService',
//   message: 'Failed to authenticate user_id=12345 ip=192.168.1.100'
// }

// Extract key=value pairs from the message
const kvPairs = {};
for (const match of groups.message.matchAll(/(\w+)=([\w.]+)/g)) {
    kvPairs[match[1]] = match[2];
}
console.log(kvPairs); // { user_id: '12345', ip: '192.168.1.100' }

6.2 Parse CSV with Quoted Fields

javascript
function parseCSVLine(line) {
    const regex = /(?:^|,)("(?:[^"]|"")*"|[^,]*)/g;
    const fields = [];

    for (const match of line.matchAll(regex)) {
        let value = match[1];
        // Remove surrounding quotes and unescape doubled quotes
        if (value.startsWith('"') && value.endsWith('"')) {
            value = value.slice(1, -1).replace(/""/g, '"');
        }
        fields.push(value);
    }
    return fields;
}

const csvLine = 'John,"Doe, Jr.",30,"He said ""hello"""';
console.log(parseCSVLine(csvLine));
// ['John', 'Doe, Jr.', '30', 'He said "hello"']

6.3 Extract Markdown Links

javascript
function extractLinks(markdown) {
    const regex = /\[(?<text>[^\]]+)\]\((?<url>[^)]+)\)/g;
    const links = [];

    for (const match of markdown.matchAll(regex)) {
        links.push({
            text: match.groups.text,
            url: match.groups.url
        });
    }
    return links;
}

const md = `
Check out [Google](https://google.com) and 
[MDN Web Docs](https://developer.mozilla.org) for references.
Also see [my blog](/blog/javascript-regex).
`;

console.log(extractLinks(md));
// [
//   { text: 'Google', url: 'https://google.com' },
//   { text: 'MDN Web Docs', url: 'https://developer.mozilla.org' },
//   { text: 'my blog', url: '/blog/javascript-regex' }
// ]

6.4 Sanitize & Transform User Input

javascript
// Remove HTML tags (basic sanitizer)
function stripHTML(html) {
    return html.replace(/<[^>]*>/g, '');
}

console.log(stripHTML('<p>Hello <strong>World</strong></p>'));
// 'Hello World'

// Convert camelCase to kebab-case
function camelToKebab(str) {
    return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
}

console.log(camelToKebab('backgroundColor'));  // 'background-color'
console.log(camelToKebab('borderTopWidth'));   // 'border-top-width'
console.log(camelToKebab('fontSize'));         // 'font-size'

// Convert kebab-case to camelCase
function kebabToCamel(str) {
    return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
}

console.log(kebabToCamel('background-color'));  // 'backgroundColor'
console.log(kebabToCamel('border-top-width')); // 'borderTopWidth'

6.5 Parse Environment Variables

javascript
function parseEnvFile(content) {
    const regex = /^(?<key>[A-Z_][A-Z0-9_]*)=(?<value>"[^"]*"|'[^']*'|[^\s#]+)/gm;
    const env = {};

    for (const match of content.matchAll(regex)) {
        let value = match.groups.value;
        // Strip surrounding quotes
        if ((value.startsWith('"') && value.endsWith('"')) ||
            (value.startsWith("'") && value.endsWith("'"))) {
            value = value.slice(1, -1);
        }
        env[match.groups.key] = value;
    }
    return env;
}

const envFile = `
DATABASE_URL="postgres://localhost:5432/mydb"
API_KEY=sk-abc123def456
PORT=3000
# This is a comment
NODE_ENV='production'
`;

console.log(parseEnvFile(envFile));
// {
//   DATABASE_URL: 'postgres://localhost:5432/mydb',
//   API_KEY: 'sk-abc123def456',
//   PORT: '3000',
//   NODE_ENV: 'production'
// }

7. Search & Replace Patterns

7.1 Replace with Functions

javascript
// Censor bad words
function censor(text, badWords) {
    const regex = new RegExp(`\\b(${badWords.join('|')})\\b`, 'gi');
    return text.replace(regex, match => '*'.repeat(match.length));
}

console.log(censor('This is damn bad!', ['damn', 'bad']));
// 'This is **** ***!'

7.2 Template Engine

javascript
function renderTemplate(template, data) {
    return template.replace(
        /\{\{(\w+)\}\}/g,
        (match, key) => data[key] !== undefined ? data[key] : match
    );
}

const template = 'Hello {{name}}, welcome to {{company}}! Your role is {{role}}.';
const result = renderTemplate(template, {
    name: 'Alice',
    company: 'TechCorp'
});

console.log(result);
// 'Hello Alice, welcome to TechCorp! Your role is {{role}}.'

7.3 Smart Slug Generator

javascript
function slugify(text) {
    return text
        .normalize('NFD')                    // Decompose accents
        .replace(/[\u0300-\u036f]/g, '')     // Remove accent marks
        .toLowerCase()
        .replace(/[^a-z0-9\s-]/g, '')       // Remove special characters
        .replace(/\s+/g, '-')               // Spaces to hyphens
        .replace(/-+/g, '-')                // Collapse multiple hyphens
        .replace(/^-|-$/g, '');             // Trim leading/trailing hyphens
}

console.log(slugify('Héllo Wörld! @2026'));  // 'hello-world-2026'
console.log(slugify('JavaScript & TypeScript: A Guide'));
// 'javascript--typescript-a-guide' → 'javascript-typescript-a-guide'

7.4 Syntax Highlighter (Simplified)

javascript
function highlightJS(code) {
    const rules = [
        { regex: /(\/\/.*$)/gm,                          class: 'comment' },
        { regex: /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)/g, class: 'string' },
        { regex: /\b(\d+(?:\.\d+)?)\b/g,                 class: 'number' },
        { regex: /\b(const|let|var|function|return|if|else|for|while|class|import|export|from|async|await)\b/g, class: 'keyword' },
        { regex: /\b(true|false|null|undefined|NaN)\b/g, class: 'literal' },
    ];

    let result = code;
    for (const { regex, class: cls } of rules) {
        result = result.replace(regex, `<span class="${cls}">$1</span>`);
    }
    return result;
}

8. Unicode & Internationalization

8.1 Matching Unicode Characters

javascript
// Match emoji
const text = 'Hello 👋 World 🌍!';

// Without 'u' flag — doesn't work correctly with surrogate pairs
const emoji = text.match(/[\u{1F600}-\u{1F9FF}]/gu);
console.log(emoji); // ['👋', '🌍']

// Unicode property escapes (ES2018)
const allEmoji = text.match(/\p{Emoji_Presentation}/gu);
console.log(allEmoji); // ['👋', '🌍']

8.2 Matching Scripts & Categories

javascript
// Match any letter from any language
const text = 'Hello مرحبا こんにちは 你好';

const letters = text.match(/\p{Letter}+/gu);
console.log(letters);
// ['Hello', 'مرحبا', 'こんにちは', '你好']

// Match only Arabic script
const arabic = text.match(/\p{Script=Arabic}+/gu);
console.log(arabic); // ['مرحبا']

// Match CJK (Chinese/Japanese/Korean)
const cjk = text.match(/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]+/gu);
console.log(cjk); // ['こんにちは', '你好']

9. Performance Tips

9.1 Compile Once, Use Many Times

javascript
// ❌ BAD: Regex compiled on every call
function findEmails(texts) {
    return texts.filter(t => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t));
}

// ✅ GOOD: Regex compiled once
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
function findEmails(texts) {
    return texts.filter(t => emailRegex.test(t));
}

9.2 Avoid Catastrophic Backtracking

javascript
// ❌ DANGEROUS: Exponential backtracking on non-matching input
const badRegex = /^(a+)+$/;
// This hangs on: 'aaaaaaaaaaaaaaaaaaaaaa!'

// ✅ SAFE: Simplified, no nested quantifiers
const goodRegex = /^a+$/;
🛑 caution

[!CAUTION] Nested quantifiers like (a+)+, (a*)*, or (a|a)* can cause catastrophic backtracking — the regex engine tries exponentially many paths. Always test your patterns with non-matching inputs.

9.3 Use Specific Patterns

javascript
// ❌ SLOW: Overly broad
const slow = /.*@.*\..*/;

// ✅ FAST: Specific character classes
const fast = /[^\s@]+@[^\s@]+\.[^\s@]+/;

10. Quick Interview Q&A

QuestionAnswer
test() vs match()?test() returns boolean, match() returns match array or null. Use test() for simple validation.
Greedy vs lazy?Greedy (*, +) matches as much as possible; lazy (*?, +?) matches as little as possible.
What is a lookahead?(?=...) asserts what follows without consuming characters. Useful for complex validations.
Named groups?(?<name>pattern) creates a named capture accessible via match.groups.name.
Why double-escape in new RegExp()?Strings process \ first. \d in a string is just d. Use \\d to pass \d to the regex engine.
What causes catastrophic backtracking?Nested quantifiers like (a+)+ create exponential paths. Simplify or use atomic groups.
match() vs matchAll()?match(/g/) returns strings only. matchAll() returns an iterator with full match objects including groups.
What does the u flag do?Enables full Unicode support. Required for \p{...} property escapes and correct surrogate pair handling.

Regex Cheat Sheet

CHARACTERS
.         Any character (except newline)
\d  \D    Digit / Non-digit
\w  \W    Word char / Non-word char
\s  \S    Whitespace / Non-whitespace
\b        Word boundary

QUANTIFIERS
*         0 or more (greedy)
+         1 or more (greedy)
?         0 or 1 (greedy)
*?  +?    Lazy versions
{n}       Exactly n
{n,m}     Between n and m

GROUPS
(...)     Capture group
(?:...)   Non-capture group
(?<n>..)  Named group
\1        Backreference

ASSERTIONS
^    $    Start / End of string
(?=...)   Positive lookahead
(?!...)   Negative lookahead
(?<=...)  Positive lookbehind
(?<!...)  Negative lookbehind

FLAGS
g  i  m  s  u  d  v

Conclusion

Regular expressions are a critical skill that separates developers who struggle with text processing from those who handle it with ease:

  1. Validation: Use regex for emails, passwords, phone numbers, and URLs — but know the limitations
  2. Extraction: Named groups + matchAll() make pulling structured data from text effortless
  3. Transformation: replace() with functions enables powerful text processing pipelines
  4. Lookaround: Zero-width assertions let you validate complex conditions without over-matching
  5. Performance: Compile once, avoid nested quantifiers, and use specific character classes

The patterns in this guide cover the vast majority of regex use cases you'll encounter in real-world JavaScript development. Bookmark the cheat sheet, practice with the examples, and regex will become one of your most valued tools.

Happy coding! 🚀

🧠 Test Your Knowledge

Now that you've learned the concepts, let's see if you can apply them! Take this quick quiz to test your understanding.

PreviousJavaScript String Methods Mastery: Complete Guide from slice() to replaceAll()NextTwo Pointers Algorithm Mastery: The Complete Intensive Guide

Written by

Shaik Munsif

Read more articles

Found this helpful? Share it with your network!

On this page

0/46
Question 1 of 10Easy
Score: 0/0

What does \d match in a regular expression?