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.
Practice now
Study without an account
^ and $ match?Scheduled with the default Askesis timings. Progress is saved in this browser.
All 50 cards
- 1What do
^and$match?The start and the end of the input (of each line with themflag) - 2What does
\bmatch?A word boundary: a position between a word character and a non-word character (or the edge) - 3
\d,\w,\sA digit, a word character ([A-Za-z0-9_]), whitespaceUppercase versions (\D,\W,\S) match the opposite. - 4What does
.match?Any character except a line breakWith thes(dotAll) flag it matches line breaks too. - 5What does
[^abc]match?Any single character excepta,borc - 6Match one lowercase letter or a digit
[a-z0-9] - 7How do you match a literal
-inside a character class?Put it first or last, or escape it:[-a-z],[a-z-],[a-z\-] - 8
*,+,?Zero or more, one or more, zero or one - 9
{3},{2,},{2,5}Exactly 3, at least 2, between 2 and 5 (inclusive) - 10Greedy vs lazy quantifiersGreedy (
.*) matches as much as possible; lazy (.*?) as little as possible - 11What does
<.+>match in<b>bold</b>, and how do you match just<b>?The whole string<b>bold</b>(greedy). Use<.+?>or<[^>]+> - 12Capturing group vs non-capturing group
(abc)captures the match for later use;(?:abc)only groups - 13Named capture group
(?<year>\d{4}), read asmatch.groups.yearin JavaScript - 14What does
\1mean inside a pattern?A backreference: the same text the first group matched(\w)\1matches doubled letters likelloroo. - 15Backreference to a named group
\k<name>, e.g.(?<q>['"]).*?\k<q>matches a quoted string - 16What does
cat|dogmatch?EithercatordogAlternation has the lowest precedence:^cat|dog$means(^cat)|(dog$). - 17Positive lookahead
x(?=y)matchesxonly ifyfollows, without consumingy - 18Negative lookahead
x(?!y)matchesxonly ifydoes not follow - 19Lookbehind
(?<=\$)\d+matches digits preceded by a dollar sign;(?<!…)is the negative form - 20Password check: at least one digit and 8+ characters, using lookahead
^(?=.*\d).{8,}$ - 21The
gflagGlobal: find all matches instead of stopping at the first - 22The
iflagCase-insensitive matching - 23The
mflagMultiline:^and$match at line breaks, not only at the ends of the input - 24The
sflagdotAll:.also matches line breaks - 25The
uandvflagsUnicode mode: correct handling of code points and\p{…}property escapes;vadds set operations - 26The
yflagSticky: the match must start exactly atlastIndex - 27Match any letter in any script
\p{L}with theuflag, e.g./\p{L}+/u - 28Which characters need escaping outside a class?
. * + ? ^ $ { } ( ) | [ ] \ /(the slash only in a/…/literal) - 29Escape user input for use in a RegExpjs
const escaped = input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') new RegExp(escaped)Newer runtimes also haveRegExp.escape(input). - 30Trim leading and trailing whitespace
^\s+|\s+$with thegflag, replaced by an empty stringIn JavaScript just usestr.trim(). - 31Match an ISO date like 2026-10-15
^\d{4}-\d{2}-\d{2}$This checks the shape only; it accepts 2026-13-40. - 32A pragmatic email check
^[^\s@]+@[^\s@]+\.[^\s@]+$Fully validating email by regex is impractical; send a confirmation link instead. - 33Collapse runs of whitespace into one spacejs
text.replace(/\s+/g, ' ') - 34Get every match with its groupsjs
for (const m of text.matchAll(/(?<key>\w+)=(?<value>\w+)/g)) { console.log(m.groups.key, m.groups.value) }matchAllrequires thegflag. - 35Reorder a date with capture groups in
replacejs'2026-10-15'.replace(/(\d+)-(\d+)-(\d+)/, '$3/$2/$1') // '15/10/2026' - 36Use a named group in a replacement string
$<name>, e.g..replace(/(?<user>\w+)@/, '$<user> at ') - 37What does
$&mean in a replacement?The whole matched text - 38
testvsmatchvsexectestreturns a boolean;str.matchreturns matches;execreturns one match with groups and advanceslastIndexwithg - 39Why does a
gregex reused withtest()alternate true and false?It keepslastIndexbetween calls, so the next search starts after the previous matchDrop thegflag for yes/no checks, or resetlastIndex = 0. - 40Split on commas with optional spacesjs
'a, b,c'.split(/\s*,\s*/) // ['a', 'b', 'c'] - 41What is catastrophic backtracking?Exponential running time when nested quantifiers can match the same text in many ways, as in
(a+)+$againstaaaa…b - 42How do you avoid catastrophic backtracking?Avoid nested quantifiers over overlapping text, make alternatives mutually exclusive, and prefer specific classes like
[^"]*over.* - 43Atomic groups and possessive quantifiers
(?>…)anda++never give back what they matched, preventing backtrackingSupported by PCRE, Java and .NET; JavaScript has no possessive quantifiers. - 44What does
\Bmatch?A position that is not a word boundary\Bcat\Bfindscatinsideconcatenatebut not the wordcat. - 45Whole-word search for
cat\bcat\b - 46Match a hex color like #1a2B3c
^#(?:[0-9a-fA-F]{3}){1,2}$Accepts the 3- and 6-digit forms. - 47Match an empty line
^$with themflag - 48POSIX character class for digits
[[:digit:]]Used by grep, sed and awk; JavaScript doesn’t support POSIX classes. - 49grep: basic vs extended regexIn basic (default) mode
+ ? | { } ( )need backslashes;grep -Etreats them as operators - 50Which regex flavors allow lookbehind of any length?JavaScript and .NET; Python’s
reneeds 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.
More in Programming
JavaScript
Master the JavaScript every developer uses daily: scope, closures, equality, array methods, promises, async/await and the event loop.
50 cardsAdvanced 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 cardsTypeScript
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 cardsPython
Learn idiomatic Python fast: comprehensions, f-strings, args and kwargs, generators, decorators, context managers and the standard library.
50 cardsGit & terminal
The Git commands and shell skills developers use every day: branching, rebasing, undoing mistakes, pipes, redirects, permissions and processes.
50 cardsJava
Core Java with short snippets: equals vs ==, records, sealed types, collections, generics, exceptions, streams, lambdas and JVM basics.
50 cardsC#
C# and .NET fundamentals with short snippets: value vs reference types, properties, records, nullable reference types, LINQ, async/await and DI.
50 cardsKotlin
Kotlin fundamentals with short snippets: null safety, val vs var, data and sealed classes, extension functions, scope functions and coroutines.
50 cardsSwift
Swift fundamentals with short snippets: optionals, structs vs classes, protocols, enums with associated values, closures, ARC, async/await and SwiftUI.
50 cardsFlutter
Flutter and Dart basics: null safety, futures and streams, stateless vs stateful widgets, setState, hot reload, keys, layout and state management.
50 cardsPHP
Modern PHP 8 with short snippets: strict types, arrays, match, nullsafe and named arguments, enums, readonly, Composer, PDO and password_hash.
50 cardsRuby
Ruby fundamentals with short snippets: blocks, procs and lambdas, symbols, Enumerable, mixins, truthiness, method_missing, gems and Bundler.
50 cardsGo
Go (Golang) fundamentals with short, idiomatic snippets: slices vs arrays, maps, defer, error wrapping, interfaces, goroutines, channels, select and context.
50 cardsRust
Learn the Rust that the compiler keeps asking about: ownership, borrowing, lifetimes, Option and Result, traits, closures, smart pointers and async.
50 cardsC++
Modern C++ in short snippets: references vs pointers, RAII, smart pointers, move semantics, STL containers and algorithms, const, templates and UB.
50 cardsHaskell
Haskell fundamentals with short code: types and type classes, pattern matching, laziness, Maybe and Either, folds, Functor, Applicative and Monad.
50 cardsElixir
Elixir fundamentals with short snippets: immutability, pattern matching, the pipe operator, Enum vs Stream, processes, GenServer, supervisors and mix.
50 cardsx86-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