Minifyr

Developer

Regex Cheat Sheet for Common Patterns

A concise reference for JavaScript regular expressions, with examples and debugging advice.

By Minifyr Team · 2026-11-02 · 4 min read

What Regular Expressions Are

A regular expression is a pattern that describes a set of strings. It is used to search, validate, extract and replace text. Regular expressions are powerful but terse: a short pattern can encode a lot of behavior, which makes testing essential. This cheat sheet focuses on JavaScript's RegExp engine, the flavor used by the Regex Tester.

Flags

  • g — global: find all matches, not just the first.
  • i — ignore case.
  • m — multiline: ^ and $ match line boundaries.
  • s — dotall: . matches newlines.
  • u — Unicode-aware matching for code points and escapes.
  • y — sticky: match starting at the current position.

Flags change the meaning of a pattern. If a pattern works on one sample but not on a document, check the flags first.

Building Blocks

Character classes

  • \d digit, \w word character, \s whitespace.
  • \D, \W, \S are their negations.
  • [abc] any of a, b or c; [^abc] any character except those.
  • . any character except a newline (unless dotall is enabled).

Quantifiers

  • * zero or more, + one or more, ? zero or one.
  • {3} exactly three, {2,5} between two and five.
  • Add ? for lazy matching, for example .*?.

Anchors

  • ^ start of string or line, $ end of string or line.
  • \b word boundary, useful for whole-word matching.

Groups and alternation

  • (...) capturing group.
  • (?:...) non-capturing group.
  • a|b alternation: match a or b.

Lookarounds

Lookarounds assert context without consuming it: (?=...) positive lookahead, (?!...) negative lookahead, (?<=...) positive lookbehind and (?<!...) negative lookbehind. For example, \d+(?= USD) matches digits followed by “ USD” without including the suffix.

Common Patterns

Email-like strings

[^\s@]+@[^\s@]+\.[^\s@]+

This finds email-shaped text. It is not a complete RFC validator; use it for practical extraction, not as the only validation on a server.

URLs

https?://[^\s]+

A simple pattern for detecting http and https URLs in text. It will include trailing punctuation, so trim or refine if needed.

Whitespace-only lines

^\s*$

With the multiline flag, this matches empty or whitespace-only lines.

Whole numbers

\b\d+\b

Word boundaries avoid matching digits inside longer tokens.

Dates in ISO format

\b\d{4}-\d{2}-\d{2}\b

This matches the shape of an ISO date. It does not validate that the month or day exists; add explicit range checks if needed.

Regex vs Wildcards

Wildcards such as * and ? in shell globs are simpler than regular expressions: they match filenames, not arbitrary text. A regex can express repetition, alternatives, groups and assertions, which is why it is more powerful and easier to get wrong. Use a glob when you are matching file names in a shell, and use a regex when you need structured text matching.

Replacement References

In replacement strings, $1, $2 and so on refer to capture groups. $& refers to the entire match. If you need a literal dollar sign, escape it. Replacement syntax is handled by the tool that performs the replacement, not by the regex itself; check the documentation for the language or editor you are using.

Testing with Real Data

Patterns that work on toy samples can fail on production text. Test with Unicode, empty strings, newlines, long lines, trailing punctuation and unexpected whitespace. Include at least one input that should not match, and verify that the pattern rejects it. Keep samples small enough to debug but representative of real traffic.

For Unicode text, enable the u flag and be careful with character classes that assume ASCII. A pattern for letters should decide whether accented characters and emoji are in scope. Test the actual languages and formats your application receives. If a pattern takes noticeably long on a large input, simplify it or add limits before running it in production.

Debugging and Performance

  • Start with a small sample that contains one expected match.
  • Test with and without the global flag.
  • Escape literal special characters.
  • Check anchors and multiline mode for multi-line text.
  • Avoid ambiguous nested quantifiers that can cause catastrophic backtracking.
  • Test performance on realistic input, not only tiny samples.

A Practical Workflow

  1. Paste a sample into the Regex Tester.
  2. Write the pattern and enable the flags you need.
  3. Inspect matches, indexes and capture groups.
  4. When the pattern is ready, apply it with Find and Replace.
  5. Compare the result with the original using Text Diff.

Frequently asked questions

Which regex flavor does Minifyr use?

The Regex Tester uses the browser's native JavaScript RegExp engine.

What does the global flag do?

It returns all matches instead of stopping at the first one.

How do I test a regex safely?

Use a small sample, check flags and escaping, and avoid ambiguous nested quantifiers on large input.

Tools used in this guide

Related guides