· 3 min read

Regex Cheat Sheet: Learn Regular Expressions with 83 Flashcards

Learn regex with 83 flashcards: quantifiers, character classes, lookarounds, backreferences, flags and 25 ready patterns for email, URL, IPv4 and dates.

Magnifying glass scanning regex symbols among floating flashcards

Studying. But it's also a video game. 🎮

Flashcards, quizzes and games that make you actually remember.

Start playing — it's free

Posted by

Regex looks like line noise until you notice it is built from about twenty symbols. The Regex deck (code JA2754, 83 cards) covers all of them, from the dot to lookbehinds, then puts them to work in about 25 real patterns: email, URL, IPv4, dates, passwords, slugs, hashtags.

Study this deck: Regex

1 / 10
Question

Regular expression (regex)

Knew itLearning

Tap the card for the answer · swipe right if you knew it, left if not

The deck comes in two passes

Cards 1-46 are short definitions. Cards 47-83 go back over the same ground with worked examples. Roughly 14 topics show up twice: dot (2 and 47), * (4 and 48), {n} (5 and 53), {n,m} (6 and 54), greedy vs lazy (19-20 and 55), lookarounds (24-27 and 62-63), backreferences (34 and 68), word boundaries (35 and 67), plus the email, URL, phone, IP and hex patterns. When two cards overlap, keep the one with the example. You end up with about 65 cards that each pull their weight.

Sample cards

FrontBack
What does \b match?A word boundary (position between word and non-word character)
[^abc]Any character except a, b, or c (negation)
Lazy quantifierMatches as little as possible: *?, +?, {n,m}?
Greedy vs non-greedy<.*> matches the whole <div>text</div>; <.*?> matches <div>
Negative lookahead(?!...), e.g. ^(?!.*word).*$ = lines not containing word
Backreference(\w+)\s+\1 matches repeated words like hello hello
Non-capturing group(?:https?|ftp):// groups without capturing
Catastrophic backtracking(a+)+b on aaaaaaaaaaaa explodes; simplify or use atomic groups
\A and \ZAbsolute start/end of string, unaffected by multiline mode

What you'll memorize

Metacharacters . ^ $ \ and the pipe for OR; quantifiers *, +, ?, {n}, {n,}, {n,m}; shorthand classes \d, \w, \s and their opposites, [\s\S]; character classes [a-z], [a-zA-Z0-9], negated [^...]; escapes \., \n, \t, \r; capturing, non-capturing and backreferences \1/$1; positive and negative lookahead and lookbehind; flags i, g, m, s; anchors \A, \Z, \z. Patterns: email, URL, domain extraction, US phone, IPv4 with 0-255 octets, YYYY-MM-DD, 24-hour HH:MM, hex colours, strong passwords, usernames, quoted strings, credit cards, scientific notation, HTML tag stripping, duplicate words, capitalised words, URL slugs.

Two fixes worth making

  • Card 65 says "S flag". The dotall flag is lowercase s (/pattern/s, re.DOTALL in Python). Uppercase \S means "non-whitespace", which is a different thing entirely. Edit the front to read s so you don't mix them up.
  • Flags and anchors depend on the language. g is JavaScript only (Python uses re.findall). Card 33 is clearer as "m: ^ and $ match at the start and end of each line". \Z behaves differently in Python and PCRE, and lookbehind support varies too (card 63 says so). Tag each card with JS, Python or PCRE, and test it at regex101.com with the right flavour picked.

How to study it

Put the pattern on the front and say what it matches; the deck mostly goes the other way. Then go through the patterns symbol by symbol. The IPv4 card (59) is a good test: if you can explain 25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?, you know alternation, character classes and optional quantifiers.

Find Regex (JA2754) in the Flip It library. Next in the category: Sorting, Searching & Graph Algorithms. You'll use regex daily in modern JavaScript.

Ready to learn faster? 🚀

Join FLIP IT for free and turn studying into a game.

Create my free account