เอกสารอ้างอิงนิพจน์ปกติที่ค้นหาและพิมพ์ได้——คลาสอักขระ, anchor, quantifier, กลุ่ม, lookaround, flag และรูปแบบสำเร็จรูป ฟรี

Character classes

12
\d
Any digit, equivalent to [0-9]
\D
Any non-digit character
\w
Word character: letter, digit or underscore
\W
Any non-word character
\s
Any whitespace (space, tab, newline)
\S
Any non-whitespace character
.
Any character except newline
[abc]
Any one of a, b or c
[^abc]
Any character except a, b or c
[a-z]
Any lowercase letter in the range
[A-Za-z0-9]
Any alphanumeric character
[\d\s]
Combine classes inside brackets

Anchors & boundaries

8
^
Start of string (or line in multiline mode)
$
End of string (or line in multiline mode)
\b
Word boundary between \w and \W
\B
Not a word boundary
\A
Start of the entire string (PCRE)
\z
End of the entire string (PCRE)
\Z
End of string, before a trailing newline (PCRE)
\G
Start of the current match attempt (PCRE)

Quantifiers

11
*
Zero or more of the preceding token
+
One or more of the preceding token
?
Zero or one (makes it optional)
{n}
Exactly n repetitions
{n,}
n or more repetitions
{n,m}
Between n and m repetitions
*?
Lazy: zero or more, as few as possible
+?
Lazy: one or more, as few as possible
??
Lazy: optional, prefers zero
{2,5}?
Lazy bounded repetition
a++
Possessive: no backtracking (PCRE)

Groups & backreferences

9
(abc)
Capturing group, stores the match
(?:abc)
Non-capturing group
(?<name>abc)
Named capturing group
(?P<name>abc)
Named group, alternate PCRE syntax
a|b
Alternation: match a or b
(red|blue)
Grouped alternation
\1
Backreference to capturing group 1
\k<name>
Backreference to a named group
$1
Reference group 1 in a replacement

Lookaround

6
(?=abc)
Positive lookahead: followed by abc
(?!abc)
Negative lookahead: not followed by abc
(?<=abc)
Positive lookbehind: preceded by abc
(?<!abc)
Negative lookbehind: not preceded by abc
\d(?=px)
Digit only if followed by px
(?<=\$)\d+
Digits only if preceded by a dollar sign

Flags & modifiers

10
g
Global: find all matches, not just the first
i
Case-insensitive matching
m
Multiline: ^ and $ match line breaks
s
Dotall: . also matches newline
u
Unicode mode (full code points)
x
Extended: ignore whitespace, allow comments
y
Sticky: match from lastIndex (JS)
(?i)
Inline case-insensitive flag (PCRE)
(?im)
Combine inline flags
(?i:abc)
Scoped inline flag for a group

Escapes & special chars

11
\.
Literal dot
\\
Literal backslash
\/
Literal forward slash (in /.../ literals)
\t
Tab character
\n
Newline (line feed)
\r
Carriage return
\f
Form feed
\0
Null character
\xFF
Character by two-digit hex code
\x{00E9}
Unicode code point by hex (PCRE)
\Qabc\E
Quote a literal block (PCRE)

Common token shortcuts

10
[0-9]
Single digit, same as \d
[a-fA-F0-9]
A single hexadecimal digit
\d+
One or more digits (whole number)
\w+
One or more word characters
\s+
One or more whitespace characters
.*
Any run of characters (greedy)
.*?
Any run of characters (lazy)
[^\s]+
One or more non-whitespace characters
\b\w+\b
A whole word
(?:\r\n|\n|\r)
Any line ending

Practical patterns

10
^[\w.+-]+@[\w-]+\.[\w.-]+$
Simple email address
https?:\/\/[^\s]+
HTTP or HTTPS URL
\b(?:\d{1,3}\.){3}\d{1,3}\b
IPv4 address
#?[a-fA-F0-9]{6}\b
Six-digit hex color
\d{4}-\d{2}-\d{2}
Date in YYYY-MM-DD format
\+?\d[\d\s-]{7,}\d
Phone number (loose)
[a-z0-9]+(?:-[a-z0-9]+)*
URL slug (lowercase, hyphens)
^\s+|\s+$
Leading or trailing whitespace (trim)
\s{2,}
Two or more consecutive spaces
<[^>]+>
An HTML tag (naive)

Unicode properties

10
\p{L}
Any kind of letter from any language
\P{L}
Any character that is not a letter
\p{N}
Any kind of numeric character
\p{Lu}
An uppercase letter
\p{Ll}
A lowercase letter
\p{P}
Any punctuation character
\p{Sc}
A currency symbol
\p{Han}
A Han (Chinese) script character
\p{Emoji}
An emoji character (where supported)
\p{Greek}
A character from the Greek script

ไม่มีรายการที่ตรงกับ “:q”


เกี่ยวกับ ชีตสรุป Regex

ชีทสรุป regex นี้เป็นเอกสารอ้างอิงกะทัดรัดสำหรับไวยากรณ์ regular expression: character class, anchor และ boundary, quantifier, group และ backreference, lookaround, flag และ modifier, escape และอักขระพิเศษ ทางลัดโทเค็นทั่วไป รูปแบบสำเร็จรูปที่ใช้งานได้จริง รวมถึง Unicode property

Regex คือตัวอย่างของไวยากรณ์แบบเขียนครั้งเดียว — โทเค็นสั้นและสับสนง่าย โดยเฉพาะ lookahead กับ lookbehind, quantifier แบบ greedy กับ lazy และรูปแบบ group ต่าง ๆ แต่ละแถวแสดงโทเค็นหรือรูปแบบหนึ่งควบคู่กับคำอธิบายภาษาธรรมดาว่ามันจับคู่อะไร

ชีทนี้จบด้วยรูปแบบที่ใช้งานได้จริงซึ่งคุณคัดลอกไปใช้ได้เลย และเช่นเดียวกับทุกชีทในที่นี้ มันฟรีและทำงานฝั่งไคลเอนต์: ค้นหาแบบเรียลไทม์ สารบัญแบบ sticky คัดลอกด้วยคลิกเดียวในทุกโทเค็น และปุ่มพิมพ์สำหรับเอกสารอ้างอิง regex แบบกระดาษ

วิธีใช้ ชีตสรุป Regex

  1. ไล่ดูหมวดต่าง ๆ ตั้งแต่ Character classes และ Anchors & boundaries ไปจนถึง Practical patterns และ Unicode properties
  2. ค้นหาโทเค็นหรือแนวคิดอย่าง lookahead, \d หรือ lazy เพื่อกรองชีทได้ทันที
  3. กระโดดไปยัง Practical patterns เมื่อคุณต้องการนิพจน์สำเร็จรูปเพื่อนำไปปรับใช้
  4. คลิกโทเค็นหรือรูปแบบ หรือไอคอนคัดลอก เพื่อคัดลอกลงในโค้ดหรือตัวทดสอบ regex ของคุณ
  5. พิมพ์ชีทออกมา — ไวยากรณ์ regex เป็นสิ่งคลาสสิกที่ควรเก็บไว้บนกระดาษ

คำถามที่พบบ่อย

สิบหมวด: character class, anchor และ boundary, quantifier, group และ backreference, lookaround, flag และ modifier, escape และอักขระพิเศษ ทางลัดโทเค็น รูปแบบที่ใช้งานได้จริง และ Unicode property

มี หมวด practical-patterns มีนิพจน์สมบูรณ์สำหรับงานจับคู่ทั่วไปที่คุณคัดลอกและปรับใช้ได้ แทนที่จะประกอบทีละโทเค็น

มี หมวด lookaround แสดง lookahead และ lookbehind ทั้งแบบบวกและลบเคียงข้างกันพร้อมคำอธิบาย — หนึ่งในมุมที่สับสนที่สุดของไวยากรณ์ regex

ไวยากรณ์ที่แสดงคือแกนหลักที่ใช้ร่วมกันอย่างกว้างขวางในเอนจินยอดนิยม ซึ่งรวมถึง character class, quantifier, group, lookaround, flag และ Unicode property escape ส่วนขยายเฉพาะเอนจินอาจแตกต่างกัน ดังนั้นให้ทดสอบโครงสร้างที่ผิดปกติในภาษาเป้าหมายของคุณ

ใช่ ฟรีทั้งหมด — ค้นหาได้ พิมพ์ออกมาได้ และแสดงผลในเบราว์เซอร์ของคุณทั้งหมด

แชร์สิ่งนี้

การค้นหายอดนิยม
regex cheat sheet regular expression syntax regex character classes regex lookahead and lookbehind regex quantifiers list regex flags reference common regex patterns regex email pattern
ต้องการความช่วยเหลือ?
พบปัญหากับเครื่องมือนี้หรือไม่? แจ้งทีมงานของเรา
รายงานปัญหา

เพิ่มเครื่องมือฟรีนี้ลงในเว็บไซต์ของคุณเอง — คัดลอกและวางโค้ดด้านล่าง