nTropi
Article·

Regular Expressions: A Practical Guide for Developers

From basic patterns to lookaheads, understand the regex features you actually need day to day.

The Basics

A regex is a pattern that matches text. /hello/ matches the literal string "hello". Add flags: i (case-insensitive), g (global), m (multiline).

Character Classes

  • \d — digit, \w — word char, \s — whitespace
  • [a-z] — range, [^abc] — negation

Quantifiers

  • * zero or more, + one or more, ? optional
  • {3} exactly 3, {2,5} between 2 and 5

Groups and Lookaheads

  • (abc) — capturing group
  • (?:abc) — non-capturing group
  • (?=abc) — positive lookahead (match if followed by)
  • (?!abc) — negative lookahead

Practical Patterns

Email:    /^[^\s@]+@[^\s@]+\.[^\s@]+$/
UUID:     /^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i
Slug:     /^[a-z0-9]+(?:-[a-z0-9]+)*$/

Test patterns live with the Regex Tester.