GameParticles
Developer

Regex Cheat Sheet for Beginners: Patterns You Actually Use

The regex constructs you actually use: character classes, quantifiers, anchors, groups and flags, with copy-ready patterns for dates, emails, slugs and cleanup.

Updated

A regular expression is a pattern that describes a set of strings. You use it to find, validate or replace text: \d{3}-\d{4} matches a phone number like 555-1234, and ^\S+@\S+\.\S+$ is a rough email check. This page covers the dozen constructs that handle almost every everyday job, with examples you can paste straight into a tester.

Literal characters and the dot

Most characters match themselves: cat matches cat inside concatenate. The dot . matches any single character except a newline, so c.t matches cat, cot and c3t. To match a literal dot, escape it: \.. The characters that need escaping are . ^ $ * + ? ( ) [ ] { } | \ and, in some flavors, /.

Character classes

  • [abc] one of a, b or c
  • [a-z] any lowercase letter; [A-Za-z0-9] any ASCII letter or digit
  • [^0-9] anything except a digit (the caret negates inside brackets)
  • \d digit, same as [0-9]; \D non-digit
  • \w word character, [A-Za-z0-9_]; \W non-word
  • \s whitespace (space, tab, newline); \S non-whitespace

Inside brackets most metacharacters lose their meaning, so [.] is a literal dot and [+*] matches a plus or a star.

Quantifiers

  • * zero or more: ab* matches a, ab, abbb
  • + one or more: ab+ matches ab, abbb, not a
  • ? zero or one: colou?r matches color and colour
  • {3} exactly three; {2,4} two to four; {2,} two or more

Quantifiers are greedy by default: on <b>bold</b>, the pattern <.+> matches the whole string, not just <b>. Add ? to make it lazy: <.+?> stops at the first >. Better still, avoid the dot and say what you mean: <[^>]+>.

Anchors and boundaries

  • ^ start of string (or line, with the m flag)
  • $ end of string (or line, with m)
  • \b word boundary: \bcat\b matches cat but not concatenate

Without anchors a validation regex is nearly useless. \d{4} matches inside 12345; ^\d{4}$ only matches exactly four digits.

Groups and alternation

Parentheses group and capture. (\d{4})-(\d{2})-(\d{2}) on 2026-10-11 captures 2026, 10 and 11 as groups 1, 2 and 3. Name them with (?<year>\d{4}) and read match.groups.year. Use (?:...) when you want grouping without capture, which keeps group numbers tidy.

The pipe is alternation: jpe?g|png|gif matches any of the three. Group it when it sits inside a larger pattern: \.(jpe?g|png|gif)$.

Flags

  • g global: find all matches, not just the first
  • i ignore case: /hello/i matches HELLO
  • m multiline: ^ and $ match at every line break
  • s dotall: . also matches newlines

The Regex Tester supports all four, highlights matches live and lists each capture group so you can see exactly what $1 would contain in a replacement.

Patterns you will actually use

  • Trim whitespace: replace ^\s+|\s+$ with nothing
  • Collapse multiple spaces: replace \s{2,} with a single space
  • Extract numbers: -?\d+(\.\d+)? with g
  • ISO date: ^\d{4}-\d{2}-\d{2}$
  • 24-hour time: ^([01]\d|2[0-3]):[0-5]\d$
  • Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$
  • Simple email: ^[^\s@]+@[^\s@]+\.[^\s@]+$ (good enough for a form; the real RFC grammar is far larger)
  • US ZIP: ^\d{5}(-\d{4})?$
  • Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$
  • Strip HTML tags: replace <[^>]*> with nothing (for quick cleanup only, never for security)
  • Swap first and last name: find ^(\w+)\s+(\w+)$, replace with $2, $1
Do not parse HTML, JSON or nested parentheses with regex. Those grammars nest arbitrarily deep, and regular expressions cannot count. Use a real parser, and keep regex for flat tokens and lines.

Escaping inside strings

A regex written inside a JavaScript or Python string gets escaped twice. The pattern \d+ becomes "\\d+" in a normal string literal, r"\d+" in a Python raw string, or /\d+/ as a JavaScript literal. Forgetting this turns \b into a backspace character and the pattern silently stops matching. When you need to match user input literally, escape it first; the String Escape tool has a regex mode that adds the backslashes for you.

Debugging a pattern that almost works

Shrink the input. Paste the one line that fails, then delete pieces of the pattern until it matches, and add them back one at a time. When a regex-based replacement produces unexpected output across a large file, run the before and after through the Diff Checker to see every changed line; a greedy quantifier eating too much usually shows up as one giant deletion.

Remember that flavors differ. JavaScript, PCRE (PHP and many editors), Python's re, Go's RE2 and POSIX grep share the basics on this page but diverge on lookbehind, possessive quantifiers and Unicode classes. Test in the engine you will ship with.

More text-processing and validation tools are in the developer category.

Try it

Open the Regex Tester, paste a sample of the text you are working with, and build the pattern piece by piece while watching the highlights update.

Tools in this guide

Frequently asked questions

What is the difference between * and + in regex?

* matches zero or more of the preceding item, so it can match nothing. + matches one or more, so at least one occurrence is required. ab* matches a, while ab+ does not.

Why does my regex match too much?

Quantifiers are greedy by default and take the longest match possible. Add ? after the quantifier to make it lazy, or replace .* with a negated class like [^>]* that cannot run past the delimiter.

Do I need the g flag?

Only if you want every match. Without g, methods like match and replace stop after the first occurrence. With g, they find or replace all of them.

Why does the same regex behave differently in Python and JavaScript?

Each language has its own regex engine. The basics are shared, but lookbehind, Unicode classes, named group syntax and some flags differ, so always test in the engine you will ship with.

More developer guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵