Patterns, Anchors & Escapes โ at a glance
A regular expression is a tiny language for describing the shape of text. This sheet collects the
pieces you will reach for most often โ what to match, how much of it, and where. Examples assume
the common /pattern/flags notation; small dialect differences exist between engines.
What kind of character to match. The vocabulary of single positions.
s flag is set).[0-9].[A-Za-z0-9_].^ inverts the class.How many times the preceding piece should occur. Greedy by default; add ? to make them lazy.
? to a quantifier makes it match as little as possible.Anchors match positions, not characters. They specify where in the string a match must occur.
m flag, matches the start of each line.m).\b โ inside a word, or between two non-word characters.m flag. Not in JavaScript.\Z (end, but allows trailing newline).Group pieces, choose between alternatives, and refer back to captured text.
match.groups.n or similar.The backslash has two jobs: strip special meaning from metacharacters, and grant it to ordinary letters.
. rather than the "any character" metacharacter.\n on Windows line endings.Modifiers that change how the entire pattern is interpreted. Written after the closing slash.
^ and $ match start and end of each line.. match newlines too.\p{...} properties.Zero-width assertions. They check what's beside the current position without consuming any characters.
Useful starting points. Tweak to taste โ every dataset has its quirks.
Try a pattern against text. Matches are highlighted in real time.