Free Tools
Blog

Regex Cheat Sheet: 12 Common Patterns You Can Copy

By Admin Editor · September 28, 2026

Regular expressions are powerful and famously hard to remember. This cheat sheet gathers the syntax you'll use most and a dozen ready-made patterns you can paste straight into your code — then test before trusting.

All patterns below use JavaScript syntax, which also works in most other languages with minor changes.

A developer typing code on a laptop

Syntax cheat sheet

Pattern Matches
. Any character except a newline
\d / \D A digit / a non-digit
\w / \W A word character (letter, digit, _) / anything else
\s / \S Whitespace / non-whitespace
^ / $ Start / end of the string (or line with m)
\b A word boundary
[abc] / [^abc] Any of a, b, c / anything except them
a* a+ a? 0 or more, 1 or more, 0 or 1
a{3} a{2,5} Exactly 3, between 2 and 5
(...) Capture group
(?:...) Group without capturing
a|b a or b

Flags

  • g — find all matches, not just the first
  • i — case-insensitive
  • m — ^ and $ match at every line
  • s — . also matches newlines
  • u — full Unicode support

Data sheets and a laptop on a desk, ready for cleaning up text data

12 patterns you can copy

  1. Email (practical check): ^[^\s@]+@[^\s@]+\.[^\s@]+$
  2. URL (http or https): https?:\/\/[^\s]+
  3. Date YYYY-MM-DD: \b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b
  4. Time HH:MM (24-hour): \b([01]\d|2[0-3]):[0-5]\d\b
  5. Hex colour: #(?:[0-9a-fA-F]{3}){1,2}\b
  6. Whole numbers: -?\d+
  7. Decimal numbers: -?\d+(\.\d+)?
  8. IPv4 address (loose): \b(?:\d{1,3}\.){3}\d{1,3}\b
  9. Repeated word ("the the"): \b(\w+)\s+\1\b with the i flag
  10. Leading/trailing spaces: ^\s+|\s+$ with g — replace with nothing
  11. Multiple spaces: {2,} — replace with a single space
  12. Blank lines: ^\s*$ with gm

A word of caution

Regex is great for finding and cleaning text, but some formats are more complex than they look. The email pattern above catches obvious typos — the only real test of an email address is sending a message to it. The IPv4 pattern accepts 999.1.1.1; tighten it if you need strict validation. Dates like 2026-02-31 pass pattern 3 — check real dates in code.

Test before you ship

  1. Open the free Regex Tester.
  2. Paste a pattern and set the flags (g, i, m and so on).
  3. Paste sample text, including examples that should not match.
  4. Matches are highlighted as you type, so you can adjust until it's right.

It runs in your browser with the same JavaScript regex engine your web code uses.

FAQ

Why doesn't my pattern match across lines? . doesn't match newlines unless you add the s flag.

Do I need to escape /? Only inside a JavaScript /.../ literal. In the tester or a RegExp string, you don't.

Try it now

Paste any of these into the free Regex Tester.

Related reading

See How to Test a Regular Expression and How to Remove Blank Lines From Text.