Regular expressions cheat sheet for beginners

Published by InstantToolDeck · Updated November 2026

A regular expression (regex) is a compact pattern for finding and extracting text. It looks cryptic at first, but a small set of building blocks covers most everyday tasks. Use this page as a cheat sheet and try each pattern in the regex tester.

Literal characters and the dot

Most characters match themselves: cat matches “cat”. The dot . matches any single character except a line break. To match a literal dot, escape it: \.

Character classes

  • \d a digit; \D anything that is not a digit
  • \w a letter, digit or underscore; \W the opposite
  • \s whitespace (space, tab, line break); \S the opposite
  • [abc] one of a, b or c; [a-z] a range; [^abc] anything except a, b, c

Quantifiers

  • * zero or more
  • + one or more
  • ? zero or one (optional)
  • {3} exactly three; {2,4} two to four; {2,} two or more

Quantifiers are greedy by default and match as much as they can. Add ? to make them lazy: .*? matches as little as possible.

Anchors and boundaries

  • ^ start of the text (or each line with the m flag)
  • $ end of the text
  • \b a word boundary, which is handy for matching whole words: \bcat\b

Groups and alternation

  • (abc) captures a group you can reuse
  • (?:abc) groups without capturing
  • a|b matches a or b

Flags

g finds all matches, i ignores case, m treats ^ and $ per line and s lets the dot match line breaks.

Practical examples

GoalPattern
A 5-digit US ZIP code\b\d{5}\b
A simple email shape[\w.+-]+@[\w-]+\.[\w.-]+
A date like 2026-11-16\d{4}-\d{2}-\d{2}
A hex color#[0-9a-fA-F]{6}\b
Extra spaces {2,}

Common pitfalls

  • Forgetting to escape special characters such as . ( ) [ ] + * ? $ ^ | when you mean them literally.
  • Greedy matches swallowing too much text. Use lazy quantifiers or more specific classes.
  • Trying to validate complex formats such as full email addresses with one pattern. Use a simple shape check, then confirm by sending a message.
  • Catastrophic backtracking on nested quantifiers such as (a+)+, which can make a pattern extremely slow.

Tips for learning

Build patterns in small steps, test against several samples including ones that should not match, and read the highlighted matches. Everything you type in the tester stays in your browser.

Frequently asked questions

What does the g flag do?

It makes the pattern find all matches in the text instead of stopping after the first.

Why is my pattern matching too much?

Quantifiers like * and + are greedy. Make them lazy with ? or use a more specific character class.

Do all programming languages use the same regex syntax?

The core is shared, but features such as lookbehind and named groups differ between languages. This tester uses JavaScript’s syntax.

← All guides