iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

Regular expressions are small languages for matching text patterns. Concise, powerful, and notorious for being write-only if you are sloppy.

Regex — what it is

EXAMPLE
// ===== The model =====
// A regex describes a SET of strings.
// An engine tries to match a string against the description, optionally extracting parts.

// ===== Tiny examples =====
/cat/.test('catwalk')                  // true
/\\d{4}-\\d{2}-\\d{2}/.test('2024-04-10')   // true (ISO date shape)
'abc 123'.match(/(\\w+)\\s+(\\d+)/);         // ['abc 123', 'abc', '123', ...]

// ===== The pieces you reach for =====
// .  any char (except newline)         ^ $  start / end (or per-line with /m)
// \\d \\w \\s   digit / word / space     [abc] [^abc]  char class
// ?  *  +  {m,n}  quantifiers           |   alternation
// ( )  capture group                    (?: )  non-capturing
// (?<name>...)  named capture           (?=...) (?!...) lookahead/negative
// \\b  word boundary                     \\1  back-reference

// ===== Flags =====
// i  case-insensitive    m  ^/$ per line    s  dotall   u  unicode
// g  global              y  sticky          d  hasIndices

// ===== Common patterns =====
// Email-ish:  ^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$
// URL host:   ^https?://([^/]+)
// ISO date:   ^\\d{4}-\\d{2}-\\d{2}$
// Hex color:  ^#([0-9a-f]{3}|[0-9a-f]{6})$
// UUID v4:    ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$

// ===== When regex wins =====
// - Quick text validation + extraction
// - Search / replace in editors
// - Log forensics + grep one-liners
// - Cleaning structured-ish text

// ===== When regex hurts =====
// - Parsing nested grammars (HTML, JSON, source code) -> use a parser
// - Long monsters with no comments -> nobody can maintain
// - Anywhere catastrophic backtracking could be triggered by input

// ===== Patterns to internalise =====
// - Anchor with ^ and $ when matching whole strings
// - Prefer non-capturing groups (?:) by default
// - Name captures when there are more than two
// - Test with positives, negatives, and edge cases every time

// ===== Pitfalls =====
// - Catastrophic backtracking: ^(a+)+$ on long inputs
// - .* across newlines without /s -> 'why doesn't it match?'
// - Email validation by regex -> good luck; send the verification email
// - Trusting regex for security (HTML strip, SQL escape) -> use a real library

Why it matters

Regex is small, sharp, and beautiful when used in scope. Master a handful of constructs, anchor strictly, test with a small list every time, and reach for a parser when grammars get nested. The shortcuts here pay you back forever in editors, scripts, and quick text wins.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// Regular expressions describe sets of strings.
// Engines: PCRE (PHP), JS RegExp, Python re, RE2 (Go), Java.
// Same core; subtle differences in lookbehind, Unicode, recursion.
Try it Yourself »

Test yourself

Q1. Regular expressions describe…
Q2. The . metacharacter matches…
Q3. Anchor ^ usually matches…

Discussion

Loading…