๐Ÿ“– Regex Cheatsheet

SyntaxDescriptionExample
.Any character except a newlinea.c โ†’ abc
*0 or more of the preceding elementab* โ†’ a, ab, abb
+1 or more of the preceding elementab+ โ†’ ab, abb
?0 or 1 of the preceding elementcolou?r โ†’ color, colour
{n}Exactly n repetitions of the preceding elementa{3} โ†’ aaa
{n,}n or more repetitions of the preceding elementa{2,} โ†’ aa, aaa
{n,m}Between n and m repetitions of the preceding elementa{2,4} โ†’ aa to aaaa
[abc]Any one of a, b, or c[abc] โ†’ a, b, c
[^abc]Any character except a, b, or c[^abc] โ†’ d, e, ...
[a-z]Any character in the range a to z[a-z] โ†’ a to z
(abc)Groups and captures a subpattern(ab)+ โ†’ ab, abab
(?:abc)Groups without capturing(?:ab)+
(?<name>abc)Named capture group(?<year>\d{4})
|OR โ€” matches either sidecat|dog โ†’ cat, dog
^Start of string (or line)^abc โ†’ abc at the start
$End of string (or line)abc$ โ†’ abc at the end
\dA digit character (same as [0-9])\d+ โ†’ 123
\DA non-digit character\D+ โ†’ abc
\wA word character (letters, digits, underscore)\w+ โ†’ abc_123
\WA non-word character\W โ†’ spaces, symbols
\sA whitespace character (space, tab, newline, etc.)\s+ โ†’ runs of whitespace
\SA non-whitespace character\S+ โ†’ runs of non-whitespace
\bA word boundary\bcat\b โ†’ matches cat only
\BA non-word-boundary position\Bcat โ†’ matches part of concat
(?=abc)Positive lookahead (position followed by abc)\d+(?=px) โ†’ the 10 in 10px
(?!abc)Negative lookahead (position not followed by abc)\d(?!px)
(?<=abc)Positive lookbehind (position preceded by abc)(?<=\$)\d+ โ†’ the 100 in $100
(?<!abc)Negative lookbehind (position not preceded by abc)(?<!\$)\d+
*?Non-greedy (shortest) repetition<.*?> โ†’ shortest tag match
\1Backreference to the first capture group(a)\1 โ†’ aa
/iCase-insensitive flag/abc/i โ†’ ABC, abc
/gGlobal flag โ€” finds all matches in the string/a/g
/mMultiline flag (^ and $ match each line)/^a/m
/sdotAll flag (. also matches line breaks)/a.b/s โ†’ a\nb

A quick reference chart of commonly used regular expression syntax and symbols, with search-based filtering. Handy for jogging your memory while writing a regex, or as a reference while reading a pattern someone else wrote.

How to use

  1. Type a keyword (e.g. digit, lookahead, capture) into the search box to filter the list.
  2. Check the "Syntax" column for how to write it and the "Description" column for what it means.
  3. Check the "Example" column for a sample regex pattern.

How the calculation works

This page is a quick reference to commonly used JavaScript regular expression syntax. Type a symbol or word in the search box to show only rows whose syntax, description or example contains it. A regular expression is a small language for describing text patterns. The syntax falls into a few groups: โ€ข Character types: . \d \w \s and character classes such as [abc] and [a-z] โ€ข Repetition (quantifiers): * + ? {n,m}; adding ? after one makes it lazy (match as little as possible) โ€ข Positions (anchors): ^ $ \b, plus lookahead (?=โ€ฆ) and lookbehind (?<=โ€ฆ) โ€ข Groups: (โ€ฆ) captures a group, which you can refer to with \1 or $1 โ€ข Flags: i (case-insensitive), g (all matches), m (multiline), s (. matches line breaks) and others In the example column, the part after "โ†’" shows what the pattern matches.

Worked example

Japanese postcode (123-4567): ^\d{3}-\d{4}$ Extract the number from "10px": \d+(?=px) โ†’ 10 (\d alone takes one character and matches only the "0") Lazy matching Text <b>bold</b> <.*> โ†’ the whole <b>bold</b> <.*?> โ†’ just <b>

Things to be aware of

  • Regex details vary slightly between languages and tools. This reference follows JavaScript (ECMAScript).
  • In JavaScript, \d and \w match only ASCII letters and digits, not full-width digits or non-Latin text.
  • To try a pattern out, use the regex tester tool to see the matches as you adjust it.

FAQ

Which programming language's regex syntax is this based on?

It's based on JavaScript (ECMAScript) regex syntax, but focuses on fundamental syntax shared across most major languages (Python, PHP, Java, etc.).

Can I actually test a regex pattern here?

This page is reference-only. Use the Regex Tester tool if you want to actually test a pattern.

Does this cover advanced features like named capture groups?

Yes, in addition to basic character classes and quantifiers, it covers advanced syntax like lookahead/lookbehind and named capture groups.