Regular Expressions (Regex) Basics

A beginner-friendly guide to regular expressions — patterns, flags, and common use cases.

Regular expressions (regex) are patterns used to match and manipulate text. They appear in JavaScript, Python, SQL, grep, and most programming languages. Mastering regex saves hours of string parsing code.

Essential patterns

  • . — any single character
  • \d — any digit (0-9)
  • \w — word character (letter, digit, underscore)
  • \s — whitespace
  • + — one or more of the preceding
  • * — zero or more of the preceding
  • {n,m} — between n and m occurrences
  • ^ — start of string, $ — end of string

Common flags

  • g — global (find all matches, not just first)
  • i — case insensitive
  • m — multiline (^ and $ match line boundaries)
Test regex patterns live