Regex Cheat Sheet & Demo

Learn and test regular expressions with an interactive demo

Regex Tester Demo
Regex Options
Match Results
Regex Cheat Sheet

CharacterDescriptionExample
.Matches any single character except newlinea.c matches "abc"
\wMatches any word character (a-z, A-Z, 0-9, _)\w+ matches "hello"
\WMatches any non-word character\W matches "@"
\dMatches any digit (0-9)\d+ matches "123"
\DMatches any non-digit\D matches "a"
\sMatches any whitespace (space, tab, newline)\s+ matches " "
\SMatches any non-whitespace\S matches "a"
\bMatches a word boundary\bcat\b matches "cat" but not "catch"
\BMatches a non-word boundary\Bcat matches "catch" but not "cat "
^Matches start of string (or line with multiline flag)^abc matches "abc" at start
$Matches end of string (or line with multiline flag)abc$ matches "abc" at end
\Escapes special characters\. matches literal "."

ClassDescriptionExample
[abc]Matches any single character in the set[aeiou] matches "a" in "cat"
[^abc]Matches any single character not in the set[^0-9] matches "a" in "a1"
[a-z]Matches any character in the range[a-z] matches "b" in "abc"
[A-Z]Matches any uppercase letter[A-Z] matches "B" in "ABC"
[0-9]Matches any digit[0-9] matches "5" in "a5b"
[a-zA-Z0-9]Matches any alphanumeric character[a-zA-Z0-9] matches "x" in "x1"

QuantifierDescriptionExample
*Matches 0 or more occurrencesa* matches "" or "aaa"
+Matches 1 or more occurrencesa+ matches "a" or "aaa"
?Matches 0 or 1 occurrencecolou?r matches "color" or "colour"
{n}Matches exactly n occurrences\d{3} matches "123"
{n,}Matches n or more occurrences\d{2,} matches "1234"
{n,m}Matches between n and m occurrences\d{2,4} matches "123"
*?, +?, ??Non-greedy (lazy) matching<.*?> matches "<p>" in "<p><span>"

GroupDescriptionExample
(...)Captures a group(\d+) captures "123" in "abc123"
(?:...)Non-capturing group(?:\d+) groups "123" without capturing
(?<name>...)Named capture group(?<year>\d{4}) captures "2023" as "year"
\1, \2Backreference to captured group(\w+)\s\1 matches "word word"
|Alternation (OR)cat|dog matches "cat" or "dog"

AssertionDescriptionExample
(?=...)Positive lookahead\w+(?=\.) matches "file" in "file.txt"
(?!...)Negative lookahead\w+(?!\.) matches "file" in "filename"
(?<=...)Positive lookbehind(?<=\$)\d+ matches "100" in "$100"
(?<!...)Negative lookbehind(?<!\$)\d+ matches "100" in "100px"

FlagDescriptionExample
gGlobal: Matches all occurrences/a/g matches all "a"s in "aba"
iIgnore case: Case-insensitive matching/a/i matches "A" or "a"
mMultiline: ^ and $ match line start/end/^a/m matches "a" at start of each line
ySticky: Matches only at lastIndex/a/y matches "a" at specific position
uUnicode: Enables full Unicode matching/\p{L}/u matches any Unicode letter
sDotall: . matches newlines/a.b/s matches "a\nb"

PatternDescriptionExample
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Matches an email addressMatches "user@example.com"
https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/[^\s]*)?Matches a URLMatches "https://example.com/path"
\+?\d{1,3}[-.\s]?\d{3}[-.\s]?\d{4}Matches a phone numberMatches "+1-555-123-4567"
#?[0-9a-fA-F]{6}Matches a hex color codeMatches "#FF0000"
\d{4}-\d{2}-\d{2}Matches a date (YYYY-MM-DD)Matches "2023-12-31"
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}Matches an IPv4 addressMatches "192.168.1.1"
Tips for Using Regex
  • Use the demo above to test patterns from the cheat sheet
  • Escape special characters with \ (e.g., \. for literal dot)
  • Enable flags like Global (g) or Ignore Case (i) in the demo for different behaviors
  • Test with varied inputs to ensure patterns are robust
  • Use named capture groups (?...) for clarity