Regex Cheatsheet
Search and filter 45+ regular expression entries — anchors, character classes, quantifiers, groups, lookarounds, flags, and common patterns. Click any card to copy.
Showing 60 of 60 entries — click any card to copy the pattern
Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,m}) match as many characters as possible while still allowing the overall pattern to succeed. Lazy quantifiers (*?, +?, {n,m}?) match as few characters as possible. For example, given the string <a><b>, the greedy pattern <.+> matches the entire string <a><b>, while the lazy pattern <.+?> matches just <a> and then <b> separately. Lazy quantifiers are essential when parsing HTML or any repeated delimiters.
What are lookaheads and lookbehinds?
Lookaheads and lookbehinds are zero-width assertions — they check for a pattern ahead or behind the current position without consuming characters. A positive lookahead (?=...) asserts that what follows matches. A negative lookahead (?!...) asserts that it does not. Lookbehinds (?<=...) and (?<!...) work the same way but look backwards. For example, foo(?=bar) matches 'foo' only when immediately followed by 'bar', but the matched text is only 'foo' — 'bar' is not included in the match.
How do I match across multiple lines?
By default, ^ and $ match the start and end of the entire string, and . does not match newline characters. To make ^ and $ match the start and end of each line, enable multiline mode (m flag in JS: /pattern/m, or re.M in Python). To make . match newlines too, enable dotall/single-line mode (s flag in JS: /pattern/s, or re.S / re.DOTALL in Python). You can combine flags: /pattern/gms in JavaScript.
What is the difference between \d and [0-9]?
In most basic cases they are equivalent — both match a single digit character 0–9. However, in Unicode-aware regex engines (e.g., JavaScript with the /u flag, or Python 3 with re.UNICODE), \d can match Unicode digit characters from other scripts (e.g., Arabic-Indic numerals, Fullwidth digits), whereas [0-9] strictly matches only ASCII digits. For data validation where you expect ASCII digits, [0-9] is more precise and unambiguous.
How do capturing groups and backreferences work?
Parentheses () create a capturing group that saves the matched text for later use. Groups are numbered left-to-right by their opening parenthesis starting at 1. You can reference them in the pattern itself with \1, \2 etc. (backreferences), or in replacement strings with $1, $2 etc. For example, (\w+)\s\1 matches a word followed by a space and the same word again — useful for detecting doubled words. Named groups (?<name>...) allow you to reference by name with \k<name> in patterns or $<name> in replacements.