askesis

Regex flashcards: 50 patterns and concepts

Learn regular expressions for good: anchors, classes, greedy vs lazy quantifiers, groups, lookarounds, flags, replacements and the backtracking trap.

ProgrammingBeginner50 cardsFree

Practice now

Study without an account

0 of 50 seen0 graduated
What do ^ and $ match?

Scheduled with the default Askesis timings. Progress is saved in this browser.

All 50 cards

  1. 1
    What do ^ and $ match?
    The start and the end of the input (of each line with the m flag)
  2. 2
    What does \b match?
    A word boundary: a position between a word character and a non-word character (or the edge)
  3. 3
    \d, \w, \s
    A digit, a word character ([A-Za-z0-9_]), whitespace
    Uppercase versions (\D, \W, \S) match the opposite.
  4. 4
    What does . match?
    Any character except a line break
    With the s (dotAll) flag it matches line breaks too.
  5. 5
    What does [^abc] match?
    Any single character except a, b or c
  6. 6
    Match one lowercase letter or a digit
    [a-z0-9]
  7. 7
    How do you match a literal - inside a character class?
    Put it first or last, or escape it: [-a-z], [a-z-], [a-z\-]
  8. 8
    *, +, ?
    Zero or more, one or more, zero or one
  9. 9
    {3}, {2,}, {2,5}
    Exactly 3, at least 2, between 2 and 5 (inclusive)
  10. 10
    Greedy vs lazy quantifiers
    Greedy (.*) matches as much as possible; lazy (.*?) as little as possible
  11. 11
    What does <.+> match in <b>bold</b>, and how do you match just <b>?
    The whole string <b>bold</b> (greedy). Use <.+?> or <[^>]+>
  12. 12
    Capturing group vs non-capturing group
    (abc) captures the match for later use; (?:abc) only groups
  13. 13
    Named capture group
    (?<year>\d{4}), read as match.groups.year in JavaScript
  14. 14
    What does \1 mean inside a pattern?
    A backreference: the same text the first group matched
    (\w)\1 matches doubled letters like ll or oo.
  15. 15
    Backreference to a named group
    \k<name>, e.g. (?<q>['"]).*?\k<q> matches a quoted string
  16. 16
    What does cat|dog match?
    Either cat or dog
    Alternation has the lowest precedence: ^cat|dog$ means (^cat)|(dog$).
  17. 17
    Positive lookahead
    x(?=y) matches x only if y follows, without consuming y
  18. 18
    Negative lookahead
    x(?!y) matches x only if y does not follow
  19. 19
    Lookbehind
    (?<=\$)\d+ matches digits preceded by a dollar sign; (?<!…) is the negative form
  20. 20
    Password check: at least one digit and 8+ characters, using lookahead
    ^(?=.*\d).{8,}$
  21. 21
    The g flag
    Global: find all matches instead of stopping at the first
  22. 22
    The i flag
    Case-insensitive matching
  23. 23
    The m flag
    Multiline: ^ and $ match at line breaks, not only at the ends of the input
  24. 24
    The s flag
    dotAll: . also matches line breaks
  25. 25
    The u and v flags
    Unicode mode: correct handling of code points and \p{…} property escapes; v adds set operations
  26. 26
    The y flag
    Sticky: the match must start exactly at lastIndex
  27. 27
    Match any letter in any script
    \p{L} with the u flag, e.g. /\p{L}+/u
  28. 28
    Which characters need escaping outside a class?
    . * + ? ^ $ { } ( ) | [ ] \ / (the slash only in a /…/ literal)
  29. 29
    Escape user input for use in a RegExp
    js
    const escaped = input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
    new RegExp(escaped)
    Newer runtimes also have RegExp.escape(input).
  30. 30
    Trim leading and trailing whitespace
    ^\s+|\s+$ with the g flag, replaced by an empty string
    In JavaScript just use str.trim().
  31. 31
    Match an ISO date like 2026-10-15
    ^\d{4}-\d{2}-\d{2}$
    This checks the shape only; it accepts 2026-13-40.
  32. 32
    A pragmatic email check
    ^[^\s@]+@[^\s@]+\.[^\s@]+$
    Fully validating email by regex is impractical; send a confirmation link instead.
  33. 33
    Collapse runs of whitespace into one space
    js
    text.replace(/\s+/g, ' ')
  34. 34
    Get every match with its groups
    js
    for (const m of text.matchAll(/(?<key>\w+)=(?<value>\w+)/g)) {
      console.log(m.groups.key, m.groups.value)
    }
    matchAll requires the g flag.
  35. 35
    Reorder a date with capture groups in replace
    js
    '2026-10-15'.replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1') // '15/10/2026'
  36. 36
    Use a named group in a replacement string
    $<name>, e.g. .replace(/(?<user>\w+)@/, '$<user> at ')
  37. 37
    What does $& mean in a replacement?
    The whole matched text
  38. 38
    test vs match vs exec
    test returns a boolean; str.match returns matches; exec returns one match with groups and advances lastIndex with g
  39. 39
    Why does a g regex reused with test() alternate true and false?
    It keeps lastIndex between calls, so the next search starts after the previous match
    Drop the g flag for yes/no checks, or reset lastIndex = 0.
  40. 40
    Split on commas with optional spaces
    js
    'a, b,c'.split(/\s*,\s*/) // ['a', 'b', 'c']
  41. 41
    What is catastrophic backtracking?
    Exponential running time when nested quantifiers can match the same text in many ways, as in (a+)+$ against aaaa…b
  42. 42
    How do you avoid catastrophic backtracking?
    Avoid nested quantifiers over overlapping text, make alternatives mutually exclusive, and prefer specific classes like [^"]* over .*
  43. 43
    Atomic groups and possessive quantifiers
    (?>…) and a++ never give back what they matched, preventing backtracking
    Supported by PCRE, Java and .NET; JavaScript has no possessive quantifiers.
  44. 44
    What does \B match?
    A position that is not a word boundary
    \Bcat\B finds cat inside concatenate but not the word cat.
  45. 45
    Whole-word search for cat
    \bcat\b
  46. 46
    Match a hex color like #1a2B3c
    ^#(?:[0-9a-fA-F]{3}){1,2}$
    Accepts the 3- and 6-digit forms.
  47. 47
    Match an empty line
    ^$ with the m flag
  48. 48
    POSIX character class for digits
    [[:digit:]]
    Used by grep, sed and awk; JavaScript doesn’t support POSIX classes.
  49. 49
    grep: basic vs extended regex
    In basic (default) mode + ? | { } ( ) need backslashes; grep -E treats them as operators
  50. 50
    Which regex flavors allow lookbehind of any length?
    JavaScript and .NET; Python’s re needs a fixed length and PCRE a bounded one

Frequently asked questions

Which regex flavor does this deck use?

JavaScript, which is close to PCRE. Cards point out where grep, PCRE and Python behave differently.

Do I need to know a programming language?

No for most cards. A few show JavaScript methods such as replace and matchAll.

Can regex validate an email address?

Only roughly. The deck includes a pragmatic pattern and explains why a confirmation email is the real test.

Intermediate

JavaScript

Master the JavaScript every developer uses daily: scope, closures, equality, array methods, promises, async/await and the event loop.

50 cards
Intermediate

Advanced JS

Fifty advanced JavaScript cards: event-loop ordering puzzles, generators and async iteration, this binding, Proxy and Reflect, modules, AbortController and coercion edge cases.

50 cards
Intermediate

TypeScript

Learn the TypeScript you use every day: narrowing, generics, keyof, mapped and conditional types, utility types, satisfies and strict mode, with code on every card.

50 cards
Beginner

Python

Learn idiomatic Python fast: comprehensions, f-strings, args and kwargs, generators, decorators, context managers and the standard library.

50 cards
Beginner

Git & terminal

The Git commands and shell skills developers use every day: branching, rebasing, undoing mistakes, pipes, redirects, permissions and processes.

50 cards
Beginner

Java

Core Java with short snippets: equals vs ==, records, sealed types, collections, generics, exceptions, streams, lambdas and JVM basics.

50 cards
Beginner

C#

C# and .NET fundamentals with short snippets: value vs reference types, properties, records, nullable reference types, LINQ, async/await and DI.

50 cards
Beginner

Kotlin

Kotlin fundamentals with short snippets: null safety, val vs var, data and sealed classes, extension functions, scope functions and coroutines.

50 cards
Beginner

Swift

Swift fundamentals with short snippets: optionals, structs vs classes, protocols, enums with associated values, closures, ARC, async/await and SwiftUI.

50 cards
Beginner

Flutter

Flutter and Dart basics: null safety, futures and streams, stateless vs stateful widgets, setState, hot reload, keys, layout and state management.

50 cards
Beginner

PHP

Modern PHP 8 with short snippets: strict types, arrays, match, nullsafe and named arguments, enums, readonly, Composer, PDO and password_hash.

50 cards
Beginner

Ruby

Ruby fundamentals with short snippets: blocks, procs and lambdas, symbols, Enumerable, mixins, truthiness, method_missing, gems and Bundler.

50 cards
Beginner

Go

Go (Golang) fundamentals with short, idiomatic snippets: slices vs arrays, maps, defer, error wrapping, interfaces, goroutines, channels, select and context.

50 cards
Intermediate

Rust

Learn the Rust that the compiler keeps asking about: ownership, borrowing, lifetimes, Option and Result, traits, closures, smart pointers and async.

50 cards
Intermediate

C++

Modern C++ in short snippets: references vs pointers, RAII, smart pointers, move semantics, STL containers and algorithms, const, templates and UB.

50 cards
Intermediate

Haskell

Haskell fundamentals with short code: types and type classes, pattern matching, laziness, Maybe and Either, folds, Functor, Applicative and Monad.

50 cards
Intermediate

Elixir

Elixir fundamentals with short snippets: immutability, pattern matching, the pipe operator, Enum vs Stream, processes, GenServer, supervisors and mix.

50 cards
Intermediate

x86-64 assembly

x86-64 assembly in Intel syntax: registers and sub-registers, flags, mov and lea, jumps, the stack frame, the System V calling convention and Linux syscalls.

50 cards