Tooling6 min read

Regular Expressions

Write efficient regular expressions for validation, extraction, and transformation. Understand lookaheads, groups, flags, and common pitfalls.

regextext-processingjavascriptparsing

Regular expressions are concise patterns for matching, extracting, and transforming text. Mastering regex syntax and when to use (and not use) regex separates engineers who write cryptic patterns from those who write maintainable, efficient text processing.

Use regex for pattern matching; don't use it for parsing structured formats (HTML, JSON, CSV - use dedicated parsers). Name your capture groups. Test with edge cases. Beware of catastrophic backtracking in user-supplied patterns.

Workflow

  1. Build patterns with character classes and quantifiersThe building blocks: understanding them makes all regex readable.
  2. Use capture groups and named groupsGroups let you extract specific parts of a match; named groups make patterns self-documenting.
  3. Apply lookaheads and lookbehindsLookaheads match positions, not characters - enabling context-sensitive matching without consuming the matched text.
  4. Use flags correctlyWrong flags cause missed matches or infinite loops.
  5. Avoid catastrophic backtrackingSome regex patterns cause exponential time complexity on certain inputs - a form of ReDoS (Regex Denial of Service).

Output Format

Named capture groups for readability, tested with edge cases (empty, max length, unicode, newlines), documented pattern intent, ReDoS analysis for public-facing patterns.

Examples

Email validation (simplified): `/^[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])?)*$/`
Named groups: `const { groups: { year, month, day } } = "2026-08-01".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)`
All matches: `[..."hello world hello".matchAll(/\b(\w+)\b/g)].map(m => m[1])` → ["hello", "world", "hello"]

Edge Cases & Gotchas

  • Unicode: \w doesn't match non-ASCII letters; use `\p{L}` with `u` flag for Unicode-aware word characters
  • URL parsing: don't use regex for URL parsing - use `new URL()` which handles all edge cases correctly
  • Stateful regex with g flag: RegExp objects with `g` flag maintain lastIndex state - calling `.test()` repeatedly on the same regex with `g` flag produces alternating results

When to Break These Rules

  • Parsing HTML/XML, JSON, CSV, or any structured format: use a dedicated parser (DOMParser, JSON.parse, csv-parse). Regex fails on nested structures and edge cases these parsers handle correctly.

Reference Files

  • MDN Regular Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
  • regex101 (tester): https://regex101.com/
  • ReDoS checker: https://devina.io/redos-checker
  • Regular-Expressions.info: https://www.regular-expressions.info/

More skills in this library

Browse all technical skills - Mobile, AI/ML, Web, Backend, Design, and Product.