Guide

Regex Basics for Beginners

Regular expressions (regex) are a powerful way to search, match, and manipulate text using patterns. They look intimidating at first but once you learn the building blocks, they become an essential tool in any developer's or power user's toolkit.

Last updated: April 10, 2026

Share:

What Is Regex?

A regular expression is a sequence of characters that defines a search pattern. Most programming languages, text editors, and command-line tools support regex for finding, replacing, and validating text.

For example, the regex \d{3}-\d{4} matches phone number patterns like 555-1234. Instead of searching for every possible 7-digit phone number, you describe the pattern once.

Essential Building Blocks

  • . (dot) — Matches any single character except newline. Example: c.t matches cat, cut, cot.
  • \d — Matches any digit (0-9). \D matches any non-digit.
  • \w — Matches any word character (letters, digits, underscore). \W matches non-word characters.
  • \s — Matches any whitespace (space, tab, newline). \S matches non-whitespace.
  • ^ — Matches the start of a string. $ matches the end.
  • [ ] — Character class. [aeiou] matches any vowel. [0-9] matches any digit. [^abc] matches anything except a, b, or c.

Quantifiers — How Many?

  • * — Zero or more. a* matches "", a, aa, aaa, etc.
  • + — One or more. a+ matches a, aa, aaa but not "".
  • ? — Zero or one (optional). colou?r matches both color and colour.
  • {n} — Exactly n times. \d{4} matches exactly 4 digits.
  • {n,m} — Between n and m times. \d{2,4} matches 2, 3, or 4 digits.
  • {n,} — At least n times. \w{3,} matches words with 3+ characters.

Groups and Alternation

  • ( ) — Capturing group. Groups parts of a pattern and captures the match for later use.
  • (?: ) — Non-capturing group. Groups without capturing.
  • | — Alternation (OR). cat|dog matches either cat or dog.
  • Backreferences: \1 refers to the first captured group. (\w+) \1 matches repeated words like "the the".

10 Practical Regex Patterns

  • Email (basic): [\w.-]+@[\w.-]+\.[a-zA-Z]{2,}
  • US phone: \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
  • URL: https?://[\w.-]+(?:/[\w./-]*)?
  • IP address: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
  • Date (YYYY-MM-DD): \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
  • Hex color: #[0-9a-fA-F]{3,6}
  • HTML tag: <([a-z]+)[^>]*>.*?</\1>
  • Whitespace trimming: ^\s+|\s+$ (use with replace to trim)
  • Duplicate words: \b(\w+)\s+\1\b
  • Password strength: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Common Mistakes to Avoid

  • Greedy vs. lazy matching: .* is greedy (matches as much as possible). .*? is lazy (matches as little as possible). Use lazy matching when extracting content between delimiters.
  • Forgetting to escape special characters: . matches ANY character. To match a literal dot, use \.
  • Over-engineering: Don't use regex to parse HTML or validate complex formats like email addresses to RFC spec. Use a dedicated parser instead.
  • Not anchoring: Without ^ and $, your pattern can match anywhere in the string. \d{5} matches any 5 consecutive digits, even inside longer strings.

Using Our Regex Tester Tool

Our free Regex Tester lets you write a pattern, paste test text, and see matches highlighted in real time. It supports JavaScript regex flags (global, case-insensitive, multiline) and shows captured groups.

Use it to learn, debug, and refine your patterns before putting them in your code. Everything runs locally in your browser — no data is sent to any server.