.*RegexForge

Regex Cheatsheet

A quick reference for common regex syntax. Click any example to open the tester with it pre-loaded.

Character Classes

.Any Character

Matches any single character (except newline, unless 's' flag)

Example: a.c
\dDigit

Matches any digit (0–9)

Example: \d+
\DNon-Digit

Matches any character that is NOT a digit

Example: \D+
\wWord Character

Matches any word character (a–z, A–Z, 0–9, _)

Example: \w+
\WNon-Word Character

Matches any non-word character

Example: \W+
\sWhitespace

Matches any whitespace (space, tab, newline)

Example: \s+
[abc]Character Set

Matches any one of the listed characters

Example: [aeiou]
[a-z]Character Range

Matches any character in the range

Example: [a-z]+
[^abc]Negated Set

Matches any character NOT in the set

Example: [^0-9]

Anchors

^Start of String

Matches at the beginning of the string (or line in multiline)

Example: ^Hello
$End of String

Matches at the end of the string (or line in multiline)

Example: world$
\bWord Boundary

Matches at a position between a word and non-word character

Example: \bcat\b
\BNon-Word Boundary

Matches at a position that is NOT a word boundary

Example: \Bcat

Quantifiers

*Zero or More

Matches 0 or more of the preceding element

Example: ab*c
+One or More

Matches 1 or more of the preceding element

Example: ab+c
?Optional

Matches 0 or 1 of the preceding element

Example: colou?r
{n}Exactly n

Matches exactly n of the preceding element

Example: \d{3}
{n,}n or More

Matches n or more of the preceding element

Example: \d{2,}
{n,m}Between n and m

Matches between n and m of the preceding element

Example: \d{2,4}

Groups

(abc)Capturing Group

Groups and captures the matched content

Example: (\d+)-(\d+)
(?:abc)Non-Capturing Group

Groups without capturing

Example: (?:abc)+
(?<name>)Named Group

Capturing group with a name

Example: (?<year>\d{4})

Lookaround

(?=abc)Positive Lookahead

Asserts that what follows matches (without consuming)

Example: \d(?=px)
(?!abc)Negative Lookahead

Asserts that what follows does NOT match

Example: \d(?!px)
(?<=abc)Positive Lookbehind

Asserts that what precedes matches

Example: (?<=$)\d+
(?<!abc)Negative Lookbehind

Asserts that what precedes does NOT match

Example: (?<!$)\d+

Flags

gGlobal Flag

Find all matches, not just the first

Example: /\d+/g
iCase-Insensitive

Case-insensitive matching

Example: /hello/i
mMultiline

^ and $ match at line boundaries

Example: /^foo/m
sDotall

. matches newlines too

Example: /a.b/s
uUnicode

Enables Unicode mode

Example: /\p{L}+/u