Skip to content
xt-tools

Regex cheatsheet

Searchable reference of regular expression syntax: character classes, anchors, groups, lookarounds and flags.

JavaScript syntax unless noted. PCRE covers PHP, grep -P and nginx; Python means the built-in re module.

131 of 131 entries

Regular expression syntax
SectionSyntaxDescription
Characters & escapes.Any character except line terminators; with the s flag, any character at all.
Characters & escapes\Escapes a metacharacter so it matches literally, e.g. \. \* \+ \? \( \) \[ \{ \| \^ \$ \\ (and \/ in a JS literal).
Characters & escapes\nLine feed (newline).
Characters & escapes\rCarriage return; Windows line endings are \r\n.
Characters & escapes\tTab.
Characters & escapes\0NUL character (JS: only when not followed by a digit).
Characters & escapes\xhhCharacter with a two-digit hex code, e.g. \x41 is A.
Characters & escapes\uhhhhUTF-16 code unit with a four-digit hex code, e.g. \u00e9 is é.
Characters & escapes\u{hhhhh}JS: any code point, e.g. \u{1F600}. Needs the u or v flag.
Characters & escapes\x{hhhh}PCRE: any code point, e.g. \x{1F600}. Python uses \U0001F600.
Characters & escapes\cXControl character, e.g. \cJ is a line feed.
Characters & escapes\Q…\EPCRE: everything between is literal. Not in JS or Python (use an escape function).
Characters & escapesabcLiteral characters match themselves; the match is case-sensitive unless the i flag is set.
Character classes[abc]One character: a, b or c.
Character classes[^abc]One character that is not a, b or c (newlines included).
Character classes[a-z]One character in a range.
Character classes[a-zA-Z0-9_-]Combine ranges and characters; a - at the start or end is literal.
Character classes[\]\\^-]Inside a class, escape ] and \; ^ is literal unless first, - unless between two characters.
Character classes\dDigit. JS and PCRE: 0–9 only. Python: any Unicode decimal digit unless the ASCII flag is set.
Character classes\DAnything except a digit.
Character classes\wWord character [A-Za-z0-9_]. Python: Unicode letters and digits too, unless ASCII flag.
Character classes\WAnything except a word character.
Character classes\sWhitespace: space, tab, line breaks, and in JS also no-break and other Unicode spaces.
Character classes\SAnything except whitespace.
Character classes[\s\S]Any character including newlines; the classic JS idiom before the s flag.
Character classes[^]JS only: any character including newlines.
Character classes\RPCRE: any line break sequence (\r\n, \n, \r and Unicode line breaks).
Character classes\hPCRE: horizontal whitespace (space, tab, no-break space…).
Character classes[[:alpha:]]POSIX class inside brackets: PCRE, grep and sed. Also [:digit:], [:space:], [:upper:]. Not JS or Python.
Character classes[\w--\d]JS v flag: set subtraction, here word characters that are not digits.
Character classes[\p{L}&&\p{ASCII}]JS v flag: set intersection, here ASCII letters.
Anchors & boundaries^Start of the input; with the m flag, start of every line.
Anchors & boundaries$End of the input; with the m flag, end of every line. PCRE and Python also match before a final newline.
Anchors & boundaries\bWord boundary: between a word character and a non-word character or the edge.
Anchors & boundaries\BNot a word boundary.
Anchors & boundaries\APCRE, Python: start of the string, regardless of the multiline flag. Not JS.
Anchors & boundaries\zPCRE (and Python 3.14+): very end of the string. Not JS.
Anchors & boundaries\ZPython: very end of the string. PCRE: end of the string or before a final newline.
Anchors & boundaries\GPCRE: the position where the previous match ended.
Anchors & boundaries^…$Anchor both ends to make the whole input match, e.g. ^\d+$ for digits only.
Quantifiers*0 or more, as many as possible (greedy).
Quantifiers+1 or more, greedy.
Quantifiers?0 or 1 (optional), greedy.
Quantifiers{n}Exactly n times.
Quantifiers{n,}n or more times.
Quantifiers{n,m}Between n and m times, inclusive.
Quantifiers{,m}Python and PCRE2 10.43+: up to m times. In JS this is not a quantifier.
Quantifiers*?0 or more, as few as possible (lazy), e.g. <.+?> matches one tag at a time.
Quantifiers+?1 or more, lazy.
Quantifiers??0 or 1, preferring 0 (lazy).
Quantifiers{n,m}?Between n and m, as few as possible.
Quantifiers*+Possessive: 0 or more, never gives characters back. PCRE, Java, Python 3.11+; not JS.
Quantifiers++Possessive 1 or more. Also ?+ and {n,m}+. Not JS.
Groups & backreferences(abc)Capturing group: groups tokens and captures the match as group 1, 2 and so on.
Groups & backreferences(?:abc)Non-capturing group: groups without capturing.
Groups & backreferencesa|bAlternation: a or b. Lowest precedence, so wrap it in a group: gr(a|e)y.
Groups & backreferences(?<name>abc)Named capturing group (JS, PCRE, .NET). Python’s re module uses (?P<name>…) instead.
Groups & backreferences(?P<name>abc)Python and PCRE syntax for a named group.
Groups & backreferences\1Backreference: the text captured by group 1, e.g. (\w)\1 matches a doubled letter.
Groups & backreferences\k<name>Backreference to a named group (JS, PCRE).
Groups & backreferences(?P=name)Python and PCRE: backreference to a named group.
Groups & backreferences(?<y>\d{4})-\d\d|\d\d-(?<y>\d{4})JS (ES2025), PCRE: the same group name may be reused in different alternatives.
Groups & backreferences(?>abc)Atomic group: once matched, never backtracks into it. PCRE, Python 3.11+; not JS.
Groups & backreferences(?|(a)|(b))PCRE branch reset: each alternative numbers its groups from the same index.
Groups & backreferences(?(1)then|else)Conditional: match then if group 1 matched, else otherwise. PCRE, Python; not JS.
Groups & backreferences(?R)PCRE recursion: match the whole pattern again here, e.g. for nested brackets.
Groups & backreferences(?1)PCRE subroutine call: reuse the pattern of group 1.
Groups & backreferences(?#comment)Inline comment, ignored. PCRE, Python; not JS.
Lookarounds(?=abc)Positive lookahead: followed by abc, without consuming it.
Lookarounds(?!abc)Negative lookahead: not followed by abc.
Lookarounds(?<=abc)Positive lookbehind: preceded by abc. JS allows any pattern; Python needs a fixed width, PCRE a bounded one.
Lookarounds(?<!abc)Negative lookbehind: not preceded by abc.
Lookarounds\d+(?=px)Example: the number in 12px, without the unit.
Lookarounds(?<=\$)\d+Example: the amount after a dollar sign.
Lookarounds\b(?!un)\w+Example: words that do not start with un.
Lookarounds^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{12,}$Example: stack lookaheads to require several things (password rules).
Lookarounds\KPCRE: drop everything matched so far from the result, like a lookbehind of any length.
Flags & modifiers/pattern/flagsJS regex literal, e.g. /colou?r/gi.
Flags & modifiersnew RegExp(source, flags)Build a regex from a string; backslashes must be doubled: new RegExp("\\d+").
Flags & modifiersgJS global: find every match. Needed for matchAll and replaceAll with a regex.
Flags & modifiersiCase-insensitive.
Flags & modifiersmMultiline: ^ and $ match at line breaks.
Flags & modifierssdotAll: . also matches line terminators.
Flags & modifiersuJS Unicode mode: code points instead of UTF-16 units, \p{…} escapes, stricter syntax.
Flags & modifiersvJS unicodeSets (ES2024): everything u does plus class set operations and \p{RGI_Emoji}.
Flags & modifiersyJS sticky: match only at lastIndex.
Flags & modifiersdJS hasIndices: report start and end indices of every group.
Flags & modifiersxPCRE and Python (re.X): extended mode, whitespace ignored and # starts a comment. Not JS.
Flags & modifiers(?i)PCRE, Python: turn on a flag inline (Python: only at the very start). Also (?m), (?s), (?x).
Flags & modifiers(?i:abc)Modifier group: case-insensitive only inside. JS (ES2025), PCRE, Python.
Flags & modifiers(?-i:abc)Turn a flag off inside a group. JS (ES2025), PCRE, Python.
Flags & modifiers(?a)Python: ASCII-only \w, \d, \s and \b (re.ASCII).
Unicode\p{L}Any letter in any script. JS (u or v flag), PCRE; Python needs the third-party regex module.
Unicode\P{L}Anything that is not a letter.
Unicode\p{Lu}Uppercase letter. \p{Ll} is lowercase.
Unicode\p{N}Any number character; \p{Nd} is a decimal digit in any script.
Unicode\p{P}Punctuation.
Unicode\p{S}Symbol, e.g. currency and math signs.
Unicode\p{M}Combining mark, e.g. accents after NFD normalisation.
Unicode\p{Zs}Space separator characters.
Unicode\p{Letter}Long property names work too: \p{Uppercase_Letter}, \p{Number}.
Unicode\p{Script=Greek}Characters of a script (short form \p{sc=Greek}).
Unicode\p{Script_Extensions=Latin}Script including shared characters (short form \p{scx=Latn}).
Unicode\p{Extended_Pictographic}Emoji and pictographic code points.
Unicode\p{Emoji_Presentation}Code points shown as emoji by default.
Unicode\p{RGI_Emoji}JS v flag: a complete emoji, including multi-code-point sequences such as flags and families.
Unicode[\q{abc|def}]JS v flag: string alternatives inside a character class.
Unicode\XPCRE: one extended grapheme cluster (a user-perceived character).
Replacement$1JS, PCRE: insert group 1. Python uses \1 or \g<1>.
Replacement$<name>JS: insert a named group. Python uses \g<name>.
Replacement$&JS: insert the whole match. Python uses \g<0>.
Replacement$`JS: insert the text before the match.
Replacement$'JS: insert the text after the match.
Replacement$$JS: a literal dollar sign.
Replacement${1}PCRE, Perl: group 1, unambiguous before digits, e.g. ${1}0.
Replacement\g<1>Python: group 1, unambiguous before digits.
Replacement\g<name>Python: insert a named group.
Replacement\1Python, sed, many editors: insert group 1.
Replacementstr.replace(re, (match, p1, offset) => …)JS: compute each replacement with a function; named groups come last.
Replacementstr.replaceAll(/x/g, "y")JS: replaceAll with a regex requires the g flag.
Replacement\U$1\EUppercase the inserted text (Perl, PCRE2 extended substitution, some editors). Not JS.
Common patterns^\s+|\s+$Leading or trailing whitespace; replace with an empty string to trim.
Common patterns\s{2,}Runs of two or more whitespace characters.
Common patterns\b(\w+)\s+\1\bDoubled word, e.g. "the the".
Common patterns^\d{4}-\d{2}-\d{2}$Shape of an ISO 8601 date (does not check the calendar).
Common patterns^#?(?:[0-9a-f]{3}|[0-9a-f]{6})$Hex colour, with the i flag.
Common patterns^[a-z0-9]+(?:-[a-z0-9]+)*$URL slug: lowercase words joined by single hyphens.
Common patterns\b\d{1,3}(?:\.\d{1,3}){3}\bShape of an IPv4 address (does not check the 0–255 range).
Common patterns^[^@\s]+@[^@\s]+\.[^@\s]+$Rough email check; real validation is better done by sending a message.
Common patterns^(?!.*foo).*$Lines that do not contain foo (with the m flag).
Common patterns\B(?=(\d{3})+(?!\d))Positions for thousands separators; replace with "," (g flag) to turn 1234567 into 1,234,567.