hackandhost

Blog · · 2 min read

Regex explainer

Test a JavaScript regular expression against your own text and read the pattern back as plain English, step by step.

A regular expression is easier to write than to read a week later. Type a pattern below to see it explained piece by piece, test it against your text, and preview a replacement. It uses your browser's own regex engine, so it behaves exactly like JavaScript, not PCRE or Python.

Examples

What it means

Flags: g (find every match).

  1. (?<year>\d{4})1Capture group "year":
    • \d{4}A digit (0–9), exactly 4 times
  2. -The character "-"
  3. (?<month>0[1-9]|1[0-2])2Capture group "month": one of
    • 0The character "0"
    • [1-9]One character: "1" to "9"
    • or
    • 1The character "1"
    • [0-2]One character: "0" to "2"
  4. -The character "-"
  5. (?<day>0[1-9]|[12]\d|3[01])3Capture group "day": one of
    • 0The character "0"
    • [1-9]One character: "1" to "9"
    • or
    • [12]One character: "1" or "2"
    • \dA digit (0–9)
    • or
    • 3The character "3"
    • [01]One character: "0" or "1"
Test text 74 characters
Matches
Released 2026-12-01, patched 2026-12-15. Not a date: 2026-13-40 or 26-1-1.
Groups

Nothing matched.

Replace use $1, $<name>, $&
Type a replacement to preview it.
Code
const re = /(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])/g;
// or
const re = new RegExp("(?<year>\\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\\d|3[01])", "g");

Quick reference

Characters

Pattern Matches
. any character except a line break
\d \w \s a digit, a word character, whitespace
\D \W \S the opposite of each
[abc] [^abc] one of these, or anything except these
[a-z] a range
\p{L} a letter in any script (needs the u flag)

Repeats and anchors

Pattern Meaning
* + ? zero or more, one or more, optional
{3} {2,5} {2,} exactly 3, 2 to 5, at least 2
*? +? lazy: as few as possible
^ $ start and end (of each line with the m flag)
\b a word boundary

Groups

Pattern Meaning
(…) capture
(?<name>…) named capture
(?:…) group without capturing
(?=…) (?!…) followed by, not followed by
(?<=…) (?<!…) preceded by, not preceded by
\1 \k<name> the same text a group matched

Patterns that hang

Some patterns take exponentially long on text that almost matches. The classic is a repeat inside a repeat, like (a+)+$ run against a long line of as ending in b: the engine tries every way of splitting the as between the two repeats before giving up. This is called catastrophic backtracking, and it has taken down production servers.

The tool runs matching separately from the page and stops it after 1.5 seconds, so you'll see a warning instead of a frozen tab. If you hit it, look for nested repeats and make the inner part unambiguous.