Regex Syntax Cheat Sheet: Character Classes, Quantifiers & Assertions at a Glance

正则表达式语法速查表:字符类、量词、断言一网打尽

"Every time I write regex I have to search the syntax — what does \b mean again? Is lookahead (?=) or (?<=)? Do I need to escape hyphens in character classes?" — regex has so many syntax details, it's perfectly normal to forget.

This cheat sheet organizes all common regex syntax by category with tables and examples for quick lookup when you need it. Bookmark this page and stop searching every time.

01 Character Classes

Character classes match specific categories of characters. They are the most fundamental and frequently used building blocks of regex. The table below lists all common character classes and their meanings.

Character Classes Quick Reference

SyntaxMeaningExample
.Any character except newlinea.c → abc, a1c, a-c
\dDigit [0-9]\d{3} → 123, 456
\DNon-digit [^0-9]\D+ → abc, hello
\wWord char [a-zA-Z0-9_]\w+ → hello_world
\WNon-word character\W → @, #, !, space
\sWhitespace (space, tab, newline)a\sb → "a b", "a\tb"
[abc]Character set — matches a, b, or c[aeiou] → matches vowels
[^abc]Negated set — any char except a, b, c[^0-9] → matches non-digits
[a-z]Range — lowercase a through z[A-Za-z] → all letters

02 Quantifiers

Quantifiers control how many times the preceding character or group can repeat. They default to greedy mode (match as much as possible); add ? after the quantifier for lazy mode (match as little as possible).

Quantifiers Quick Reference

SyntaxMeaningLazy Version
*Zero or more*?
+One or more+?
?Zero or one??
{n}Exactly n times
{n,}n or more times{n,}?
{n,m}Between n and m times{n,m}?

03 Anchors & Boundaries

Anchors don't match characters themselves but match positions between characters. They constrain matches to specific locations (beginning of line, end of line, word boundaries).

Anchors Quick Reference

SyntaxMeaningExample
^Start of line (or string)^Hello → only Hello at line start
$End of line (or string)world$ → only world at line end
\bWord boundary\bcat\b → matches "cat" not "catch"
\BNon-word boundary\Bcat → matches "cat" in "scat"

04 Groups & Assertions

Groups use parentheses to combine multiple characters into a single unit for repetition, capturing, or conditional matching. Assertions (also called zero-width assertions) match positions without consuming characters.

Groups & Assertions Quick Reference

SyntaxMeaningExample
(abc)Capturing group(\d+)px → captures the digits
(?:abc)Non-capturing group(?:jpg|png) → groups without capturing
(?<name>abc)Named capturing group(?<year>\d{4}) → groups["year"]
a|bAlternation (or)cat|dog → matches cat or dog
(?=abc)Positive lookahead\d+(?=px) → digits followed by px
(?!abc)Negative lookahead\d+(?!px) → digits NOT followed by px
(?<=abc)Positive lookbehind(?<=\$)\d+ → digits after $
(?<!abc)Negative lookbehind(?<!\$)\d+ → digits NOT after $

05 Special Sequences & Escapes

Some characters in regex have special meanings. To match these characters literally, escape them with a backslash \. Below is a list of special characters and sequences to remember.

  • Special characters requiring escape: . * + ? ^ $ { } [ ] ( ) | \
  • \n — newline
  • \t — tab character
  • \r — carriage return
  • \0 — null character
  • \uXXXX — Unicode character (e.g., \u4e2d matches "中")
  • \p{Script=Han} — Unicode property match (requires u flag, matches Chinese characters)

FAQ

What's the difference between \d and [0-9]?

In JavaScript, \d is equivalent to [0-9] by default. But in some engines with Unicode mode (u flag), \d may match digit characters from other scripts (like Arabic ٣). If you only want 0-9, explicitly use [0-9].

How do I match Chinese characters?

Use Unicode property matching: /\p{Script=Han}/u. The traditional [\u4e00-\u9fa5] range only covers the CJK Unified Ideographs basic block and may miss extension characters. Unicode property matching is more accurate and comprehensive.

Are there limitations on lookahead and lookbehind?

In JavaScript (ES2018+), both lookahead and lookbehind are fully supported. But in some older engines or languages, lookbehinds may not support variable-length patterns. Suried Regex Tester uses a modern JavaScript engine with full assertion support.

Does ^ have two different meanings in regex?

Yes. Outside character classes [...], ^ is a start-of-line anchor; inside [...] at the beginning, ^ negates the class. For example, ^abc matches "abc" at line start, while [^abc] matches any character except a, b, or c.

Does this cheat sheet cover all regex syntax?

This sheet covers over 95% of regex syntax needed in daily development. Some advanced features like conditional matching (?(1)a|b), atomic groups (?>...), and recursive patterns are PCRE extensions not natively supported in JavaScript and are not included.

📋

Try the Tool Now

This cheat sheet organizes all common regex syntax by category with tables and examples for quick lookup when you need it. Bookmark this page and stop searching every time.

TOOLS.SURIED.COM