What is Regex?

A regular expression (regex) is a sequence of characters that defines a search pattern — used to match, extract, validate, and replace text in code, terminals, and text editors.

Definition

Regex is supported in virtually every programming language (JavaScript, Python, Go, Java, etc.) and most text editors. A pattern like \d{3}-\d{4} matches phone number fragments; ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$ validates an email address.

Common Syntax

  • . — matches any single character except newline
  • * — zero or more of the preceding element
  • + — one or more of the preceding element
  • ? — zero or one (makes preceding element optional)
  • ^ — start of string (or line in multiline mode)
  • $ — end of string (or line)
  • \d — any digit (0–9)
  • \w — any word character (letter, digit, underscore)
  • \s — any whitespace character
  • [abc] — character class: matches a, b, or c
  • (abc) — capture group: captures the matched text
  • a|b — alternation: matches a or b

Common Flags

  • i — case-insensitive matching
  • g — global: find all matches, not just the first
  • m — multiline: ^ and $ match start/end of each line
  • s — dotall: . matches newline characters too

Test and debug regex patterns against real text: Open Regex Tester →

Frequently Asked Questions

What does .* mean in regex?

. matches any character except newline. * means zero or more. Together .* is a greedy match for any sequence of characters.

What is a capture group?

Parentheses create a capture group: (\d{4}). The matched text is saved and can be referenced in replacements or code as $1, \1, etc.

What is the difference between greedy and lazy matching?

Greedy matching (.*) matches as much as possible. Lazy matching (.*?) matches as little as possible. Lazy mode stops at the first opportunity; greedy mode goes to the last.