Regular Expressions by Example

Regex is one of those skills that's hard to learn, easy to forget, and constantly useful. This is a working dev's reference — the parts that come up daily, with examples you can paste into the JustKit regex tester and see in action.

Anchors

Anchors aren't characters — they're positions. Match where in the string you are.

^cat$ matches the exact string "cat" but not "cats" or " cat". \bcat\b matches "cat" anywhere, including inside "the cat sat" but not inside "category".

Character classes

Quantifiers

By default, quantifiers are greedy — they match as much as possible. Adding a ? after them makes them lazy — match as little as possible. .* matches as much as it can; .*? matches as little as it can. The lazy form is what you want when extracting between delimiters: <.*> on <a>X</a> matches the entire string; <.*?> matches each tag separately.

Groups

Groups also let you apply quantifiers to multi-character sequences: (ab)+ matches one or more "ab"s.

Lookarounds (zero-width assertions)

Sometimes you need to match X but only when followed/preceded by Y — without including Y in the match.

Most modern engines (JS post-ES2018, Python, Go's regexp2, PCRE) support all four. Some older or simpler engines (Go's stdlib regexp for example) only support lookahead. Check before you ship.

The 12 patterns you'll actually use

  1. Email (good enough): ^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,}$ — pragmatic, not RFC 5322 perfect (perfect is ~6,000 characters long).
  2. URL (HTTP/HTTPS): ^https?://\S+$ — coarse but works.
  3. IPv4: ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$
  4. UUID v4: ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ (case-insensitive).
  5. Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$ — matches #fff and #ffffff.
  6. Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$ — lowercase, hyphens between, no leading/trailing/double hyphens.
  7. ISO date (YYYY-MM-DD): ^\d{4}-\d{2}-\d{2}$ — coarse; doesn't validate that 02-30 is impossible.
  8. Whitespace runs: \s+ — for collapsing multiple spaces. Replace with single space.
  9. Trailing whitespace: \s+$ with m flag — strip per line.
  10. HTML/XML tag: <([a-z][a-z0-9]*)\b[^>]*>.*?</\1> — naive but fine for snippets. Don't try to fully parse HTML with regex.
  11. Phone (loose international): ^\+?[0-9\s\-()]{7,}$ — varies massively by country; loose is best.
  12. Markdown header: ^(#{1,6})\s+(.+)$ with m flag — captures level and text.

Flags

Common mistakes

JustKit's role

The JustKit regex tester shows live matches as you type, with each match highlighted in the input. Use it to iterate on a pattern, paste in real-world test data, and verify edge cases before shipping.