Elofyn

Explainer

What is Regex Golf?

Rules, history, scoring, and the six regex concepts that unlock 90% of puzzles.

What is Regex Golf?

Regex Golf is a precision puzzle game: given two lists of words — a TARGET list and a FORBIDDEN list — write the shortest regular expression that matches every word in the TARGET list and fails to match any word in the FORBIDDEN list. The game turns an ordinary developer skill into a competitive exercise in brevity. Every character in your pattern costs one point; the player who solves the puzzle with the fewest characters wins.

The elegance lies in the tension between precision and coverage. A naïve solution might enumerate every target word with alternation: cat|dog|bat (11 chars). A sharper solution observes that all three end in a consonant followed by at and writes [cdb]at (6 chars). An even sharper one notices that nothing in the forbidden list contains at and writes simply at (2 chars). That compression — from obvious to minimal — is the skill Regex Golf trains.

On this site, the game runs entirely in your browser. The regex engine is JavaScript’s built-inRegExp, evaluated via .test(word) per word. No server is involved; no account is required. Your input never leaves your browser.

A short history

The competitive form of Regex Golf has a traceable origin. Around 2013, Swedish developer Alf Sjöblom published alf.nu/regex, a browser game presenting a series of word lists and asking players to write the shortest regex to separate them. The site spread through Hacker News and developer communities, and the format crystallised as: two lists, one pattern, lowest character count wins.

The format reached mainstream visibility on January 10, 2014, when Randall Munroe published xkcd #1313, titled “Regex Golf.”1 The comic used the names of US presidents and non-presidents as its word lists, asking readers to write a regex that matched Obama, Bush, Carter, and others, while rejecting Romney, Palin, McCain, and so on. The strip went viral, and the associated xkcd forums filled with increasingly optimised solutions.

Peter Norvig — Director of Research at Google and author of Artificial Intelligence: A Modern Approach— published a computational follow-up notebook titled “Solving Every Regex Golf Puzzle.”3 Norvig demonstrated that optimal-length regexes could be found algorithmically, using a generate-and-test search over a grammar of regex patterns. His analysis confirmed that the minimal regex for the xkcd president problem was shorter than most human attempts, and that human intuition reliably under-uses features like backreferences and negated character classes. This is why the six concepts in §4 below matter — they are the features that algorithmic solvers exploit, and most humans miss.

Since 2014 the genre has spread. Variants with different scoring rules (flag penalties, character-class restrictions, time limits) have appeared on developer platforms. The core mechanic — compress a separator regex until it is provably minimal — remains unchanged.

How scoring works

Your score is the number of characters in the raw pattern body — everything between the delimiting slashes in a JavaScript regex literal, not counting the slashes themselves. A pattern of ^[a-z]+$ scores 8. Whitespace counts; so does each backslash in an escape sequence.

Flags are penalised at +2 chars per active flag. If you enable case-insensitive mode (flag i) and multiline mode (flag m), your score increases by 4. This incentivises solving without flags where possible, since a case-insensitive shortcut like writing a with the i flag costs 3 chars total (1 pattern + 2 flag), while [aA] costs 4 chars. The flag is worth it — but only for sufficiently complex patterns where the savings exceed the 2-char penalty.

The tension this creates is the heart of the game. Consider matching vowels: [aeiou] is 7 chars versus(a|e|i|o|u) at 13 chars. The character class wins handily. Now consider matching hex digits: [0-9a-f] (8 chars) versus[0-9a-fA-F] (11 chars) with no flag versus[0-9a-f] with i flag (10 chars). The flag wins only if the pattern is already large.

Six concepts that unlock 90% of puzzles

The MDN Web Docs reference on JavaScript regular expressions2 is the authoritative source for syntax details. Here are the six constructs that appear in nearly every efficient Regex Golf solution:

  1. 1. Character classes [abc] and negation [^abc]

    Square brackets match any one character from a set. [aeiou] matches any vowel. [^aeiou] matches any non-vowel. Ranges shorten enumerations: [a-z]covers 26 chars in 5. Negated classes are underused; “everything except” is often shorter than enumerating what you allow.

  2. 2. Anchors ^ and $

    ^ asserts the start of the string (or line in multiline mode); $ asserts the end. Together they force the regex to match the whole word, preventing false positives.^cat$ matches only the exact string “cat”; catwould also match “catfish” and “wildcat”. In golf, you often omit anchors deliberately when a substring match is sufficient.

  3. 3. Quantifiers +, *, ?, {n,m}

    + is one-or-more, * is zero-or-more, ? is zero-or-one, and {n}/{n,m} enforces an exact or bounded count. A pattern like ^\d{4}$ matches exactly four digits for 8 chars, far shorter than ^\d\d\d\d$ at 10.

  4. 4. Alternation (a|b|c)

    The pipe character separates alternatives inside a group. (jpg|png|gif) matches any of the three image types. Alternation is often the solution of last resort — when no structural pattern exists across targets, enumerate them. But always check whether a character class or substring covers the same targets more compactly first.

  5. 5. Backreferences \1

    A capturing group (pattern) stores the matched text; a backreference \1 requires the same text to appear again. This is how you match palindromes (^(.).*\1$) or repeated words ((\w+) \1). Most developers under-use backreferences; in golf, any puzzle involving symmetry or repetition is a signal to reach for them.

  6. 6. Lookaheads (?=...) and (?!...)

    A lookahead asserts that a position is (or is not) followed by a pattern, without consuming characters. ab(?!c)matches “ab” when not followed by “c”. Lookaheads are the last resort when the forbidden words share a prefix with targets — you can match the prefix while excluding the problematic suffix. They cost characters ((?=...) is 5 chars of overhead) but occasionally have no shorter alternative.

Common optimization tricks

Once you know the six concepts, the practice shifts to recognising which one to apply. A few heuristics cover the majority of puzzles:

  • Ranges over enumeration. [a-z] beats listing every letter.[0-9] and \d are equivalent at the same char count, but \d composes cleanly with quantifiers.
  • Negated classes over positive ones. If targets share the absence of a feature, a negated class is shorter. ^[^aeiou]+$ (11 chars) beats enumerating every consonant.
  • Use . (dot) with care. Dot matches anything except a newline, which is broad. Useful when you need a wildcard, dangerous when it would match forbidden words. Add anchors to tighten scope.
  • Backreferences for palindromes and repeated substrings. Any puzzle where a word reads the same forwards and backwards, or contains a doubled word, is a backreference puzzle. The pattern ^(.).*\1$ matches the first and last characters, leaving whatever is in between unconstrained.
  • Lookaheads as last resort. If no structural pattern separates targets from forbidden words, and alternation is too long, a negative lookahead may be the only solution. The overhead is 5 chars ((?!...)), so the forbidden-word suffix you exclude must save at least 6.

How the daily works

The daily puzzle is seeded by the UTC calendar date using the djb2 hash function — a simple, deterministic hash invented by Dan Bernstein. The seed string is the ISO date in YYYY-MM-DD format (e.g. 2026-06-22). The hash output modulo 32 gives the puzzle index. Because the hash is a pure function of the date string, every player worldwide gets the same puzzle on the same UTC calendar day.

The daily puzzle draws from the full pool of 32 puzzles. Repeats are possible — the same puzzle may appear on two consecutive days. The daily resets at 00:00 UTC; if you have the tab open at midnight, the puzzle does not auto-refresh. Reload the page to get the next day’s challenge.

Your result is stored in localStorage under the key elofyn:regex-golf:v1. Daily records are keyed by the UTC date string, so each day’s result is stored independently. Progress is device-local only; there is no cross-device sync.

Privacy & safety

The regex engine runs in a sandboxed Web Worker entirely in your browser. Your input never leaves your browser. No pattern, no word list result, and no score is ever transmitted to Elofyn servers.localStorage is device-local — no server can read it.

Patterns run with a hard 100ms timeout. If your regex does not complete within 100 milliseconds, the worker is terminated via Worker.terminate() — the only mechanism that can interrupt a runaway .test() call mid-execution. Patterns like (a+)+b that exhibit catastrophic backtracking are stopped cleanly; your browser tab cannot freeze.

References

  1. 1. Munroe, R. (2014-01-10). xkcd #1313 — “Regex Golf”. Supports: popularisation of the format; US president / non-president word list; “shorter is better” scoring.
  2. 2. MDN Web Docs — Regular expressions (JavaScript). Authoritative reference for JS regex syntax: character classes, anchors, quantifiers, backreferences, lookaheads, flags.
  3. 3. Norvig, P. “Solving Every Regex Golf Puzzle”. Demonstrates algorithmic generation of optimal-length regexes; confirms humans under-use negated classes and backreferences.
  • Regex Tester — interactive regex matcher with explain mode; the sandbox we cut Golf’s compile pipeline from.
  • Code Typing Test — daily typing drill for source code; same “tight feedback loop on dev skill” shape.
  • Arrow Shift — keyboard reflex game seeded by UTC date, like Regex Golf’s daily.
  • Color Code Trainer — hex-code memory game; another precision-under-time-pressure puzzle for developers.
  • Chronole — guess-the-date game with Wordle-style feedback; sibling daily-puzzle format.