categories.regex.title

regex-cheatsheet

All-in-one developer toolkit with 50+ free online tools. JSON formatter, Regex tester, Base64 encoder, QR code generator, Markdown editor, Color converter, Unit converter, CSS generator, and more. 100% client-side, privacy-first.

regexcheat sheetreferencepatternsyntax

Common Patterns

Email
/^[^\s@]+@[^\s@]+\.[^\s@]+$/
Basic email validation
URL
/https?:\/\/[^\s]+/
Example: https://example.com/path
Match HTTP/HTTPS URLs
IPv4 Address
/\b(?:\d{1,3}\.){3}\d{1,3}\b/
Example: 192.168.1.1
IPv4 address pattern
Chinese Phone
/^1[3-9]\d{9}$/
Example: 13812345678
Mainland China mobile number
Date (YYYY-MM-DD)
/\d{4}-\d{2}-\d{2}/
Example: 2024-01-15
ISO date format

Character Classes

PatternNameExampleDescription
.Any charactera.c → abc, aXcMatches any single character except newline
\dDigit\d+ → 123Matches any digit (0-9)
\DNon-digit\D+ → abcMatches any non-digit character
\wWord character\w+ → hello_1Matches letters, digits, underscore
\WNon-word character\W+ → !@#Matches non-word characters
\sWhitespacea\sb → a bMatches space, tab, newline
\SNon-whitespace\S+ → helloMatches any non-whitespace character
[abc]Character set[aeiou] → a, e, iMatches any character in set
[^abc]Negated set[^0-9] → a, bMatches any character not in set
[a-z]Character range[a-z]+ → helloMatches any character in range

Quantifiers

PatternNameExampleDescription
*Zero or moreab*c → ac, abc, abbcMatches 0 or more of preceding
+One or moreab+c → abc, abbcMatches 1 or more of preceding
?Zero or oneab?c → ac, abcMatches 0 or 1 of preceding
{n}Exact counta{3} → aaaMatches exactly n occurrences
{n,}n or morea{2,} → aa, aaaMatches n or more occurrences
{n,m}Between n and ma{2,4} → aa, aaa, aaaaMatches between n and m occurrences
*?Lazy zero or morea.*?b → abNon-greedy matching
+?Lazy one or morea.+?b → abNon-greedy matching

Anchors

PatternNameExampleDescription
^Start of string^hello → hello worldMatches at start of string/line
$End of stringworld$ → hello worldMatches at end of string/line
\bWord boundary\bword\b → the wordMatches word boundary
\BNon-word boundary\Bword → swordMatches non-word boundary
(?=...)Positive lookaheada(?=b) → abMatches if followed by pattern
(?!...)Negative lookaheada(?!b) → acMatches if not followed by pattern
(?<=...)Positive lookbehind(?<=a)b → abMatches if preceded by pattern
(?<!...)Negative lookbehind(?<!a)b → cbMatches if not preceded by pattern

Groups

PatternNameExampleDescription
(...)Capturing group(ab)+ → ababCaptures matched text
(?:...)Non-capturing group(?:ab)+ → ababGroups without capturing
(?<name>...)Named group(?<year>\d{4})Captures with name
\1Backreference(\w)\1 → aa, bbMatches captured group again
a|bAlternationcat|dog → cat, dogMatches either expression

Assertions

PatternNameExampleDescription
(?=...)Positive lookahead\d(?=px) → 10pxAssert pattern follows
(?!...)Negative lookahead\d(?!px) → 10emAssert pattern does not follow
(?<=...)Positive lookbehind(?<=\$)\d+ → $100Assert pattern precedes
(?<!...)Negative lookbehind(?<!\$)\d+ → €100Assert pattern does not precede

Flags

PatternNameExampleDescription
gGlobal/a/g → a, a, aFind all matches, not just first
iCase insensitive/abc/i → ABC, abcIgnore case in matching
mMultiline/^a/m → ... a^ and $ match line boundaries
sDotAll / Singleline/a.*b/s → a b. matches newlines too
uUnicode/\u{1F600}/uEnable Unicode support