Regex Cheat Sheet
Quick reference for regular expressions: anchors, character classes, quantifiers, groups, lookarounds, flags, and ready-to-use patterns.
Anchors
| Pattern | Matches |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary (between \w and non-word) |
\B | Non-word boundary |
\A / \Z | Start / end of string (PCRE only — not JS) |
Character classes
| Pattern | Matches |
|---|---|
. | Any character except newline (or any with s flag) |
\d | Digit [0-9] |
\D | Non-digit |
\w | Word char [A-Za-z0-9_] |
\W | Non-word char |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Range a through z |
[a-zA-Z0-9] | Multiple ranges |
Quantifiers
| Pattern | Matches |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | 2 to 5 |
{2,} | 2 or more |
*? +? ?? | Lazy (match as little as possible) |
*+ ++ ?+ | Possessive (no backtracking — PCRE) |
Groups & references
| Pattern | Matches |
|---|---|
(abc) | Capture group 1 |
(?:abc) | Non-capturing group |
(?<name>abc) | Named group (use \k<name> to backreference) |
\1 \2 | Backreference to group 1, 2 |
a|b | a or b (alternation) |
Lookarounds
| Pattern | Matches |
|---|---|
(?=abc) | Followed by abc (positive lookahead) |
(?!abc) | NOT followed by abc (negative lookahead) |
(?<=abc) | Preceded by abc (positive lookbehind) |
(?<!abc) | NOT preceded by abc (negative lookbehind) |
Flags
| Flag | Effect |
|---|---|
g | Global — find all matches, not just first |
i | Case-insensitive |
m | Multiline — ^ and $ match line boundaries |
s | Dotall — . matches newline too |
u | Unicode mode |
y | Sticky (JavaScript) — match at lastIndex only |
Common ready-to-use patterns
| Use case | Pattern |
|---|---|
| Email (basic) | ^[\w.+-]+@[\w-]+\.[\w.-]+$ |
| URL (http/https) | ^https?://[^\s/$.?#].[^\s]*$ |
| IPv4 | ^(\d{1,3}\.){3}\d{1,3}$ |
| Indian phone | ^(\+91[\-\s]?)?[0]?(91)?[6-9]\d{9}$ |
| UUID v4 | ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ |
| ISO 8601 date | ^\d{4}-\d{2}-\d{2}$ |
| Hex color | ^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$ |
| Slug (URL-friendly) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
| PAN (India) | ^[A-Z]{5}[0-9]{4}[A-Z]$ |
| GSTIN (India) | ^\d{2}[A-Z]{5}\d{4}[A-Z]\d[Z]\d$ |
Test any pattern interactively in our Regex Tester.
Regex Cheat Sheet — คู่มืออ้างอิง Regular Expression ฉบับสมบูรณ์พร้อม Pattern พร้อมใช้
Regular expressions เป็นหนึ่งในทักษะที่ให้ผลตอบแทนสูงสุดในการพัฒนาซอฟต์แวร์ — regex ที่เขียนดีเพียงบรรทัดเดียวสามารถแทนที่โค้ด string parsing 50 บรรทัดได้ แต่นักพัฒนาส่วนใหญ่ใช้เพียงส่วนเล็กน้อยของความสามารถ regex ทั้งหมด โดยติดอยู่กับ pattern พื้นฐานในขณะที่งาน validation, extraction และ transformation ที่ซับซ้อนยังคงสะสม cheat sheet นี้ครอบคลุม regex toolkit ทั้งหมด ได้แก่ anchors, character classes, quantifiers, groups, lookarounds, flags และไลบรารี pattern พร้อมใช้สำหรับงาน validation ทั่วไป รวมถึงรูปแบบเฉพาะของอินเดียอย่าง PAN และ GSTIN
เลือกดูส่วนที่ตรงกับปัญหาของคุณ — anchor, character class, quantifier, group หรือ lookaround คัดลอก pattern แล้วทดสอบแบบสดๆ ใน Regex Tester ของเราเพื่อยืนยันกับ input จริงของคุณ Pattern ในเอกสารอ้างอิงนี้ใช้งานได้กับ JavaScript และ PCRE ที่มีความแตกต่างระหว่าง flavor (เช่น การรองรับ lookbehind แบบความยาวผันแปรหรือ possessive quantifiers) คอลัมน์ notes จะระบุไว้ ส่วน "Common ready-to-use patterns" ที่ด้านล่างเป็นไลบรารี copy-paste โดยตรง — email validation, URL matching, IPv4, หมายเลขโทรศัพท์อินเดีย, UUID, ISO dates, hex colours, URL slugs, PAN และ GSTIN ทั้งหมดทดสอบแล้วและพร้อมใช้งาน
Anchors (^ $ \b) ควบคุมว่าการจับคู่จะเกิดขึ้นที่ใดใน string — หากไม่มี anchors pattern จะจับคู่ที่ใดก็ได้ใน input รวมถึงการจับคู่บางส่วน Character classes (\d \w \s . [abc] [^abc]) กำหนดว่าตัวอักษรใดยอมรับได้ในแต่ละตำแหน่ง Quantifiers (* + ? {n,m} และรูปแบบ lazy ของพวกมัน) ควบคุมว่าตัวอักษรหรือกลุ่มสามารถซ้ำได้กี่ครั้ง Groups และ alternation ((abc) (?:abc) a|b) ช่วยให้จับคู่ลำดับเป็นหน่วย จับภาพ substring สำหรับการดึงออก และให้ทางเลือก pattern ที่ถูกต้องหลายแบบ ทุกอย่างอื่นใน regex — lookarounds, backreferences, flags — สร้างขึ้นบนรากฐานทั้งสี่นี้ เชี่ยวชาญสิ่งเหล่านี้แล้วคุณจะสร้าง regex ที่ต้องการได้
นักพัฒนา backend ที่ validate form input ใช้เพื่อหาหรือตรวจสอบ pattern สำหรับ email, โทรศัพท์, รหัสไปรษณีย์ และการ validate รูปแบบ ID วิศวกรข้อมูลที่เขียน ETL pipelines ใช้สำหรับ pattern การดึงและแปลงข้อความจากข้อมูล log, ไฟล์ CSV และ API responses นักวิจัยด้านความปลอดภัยใช้เมื่อเขียนกฎ WAF หรือวิเคราะห์ log pattern สำหรับการตรวจจับการบุกรุก นักพัฒนา frontend ที่สร้าง form validation ใช้ส่วน ready-to-use patterns เป็นจุดเริ่มต้นและปรับแต่งจากนั้น นักพัฒนาที่ทำงานกับแอปพลิเคชันอินเดียใช้สำหรับ pattern การ validate หมายเลข PAN และ GSTIN ที่ยากจะหาที่อื่นที่เขียนถูกต้อง
นี่คือหน้าอ้างอิงแบบ static — ไม่มีการเก็บหรือส่งข้อมูลใดๆ ทดสอบ pattern กับข้อมูลของคุณเองโดยใช้ Regex Tester ที่ลิงก์ไว้ ซึ่งทำงานในเบราว์เซอร์ของคุณทั้งหมด
คำถามที่พบบ่อย
- How do I learn regex fast?
- Memorize the four pillars: anchors (^ $ \b), character classes (\d \w \s . []), quantifiers (* + ? {n,m}), and groups (). Everything else builds on these. Test every pattern as you learn it — rote memorization without practice fails fast. Spend 30 minutes a day for two weeks and you'll cover 95% of real-world cases.
- What's the difference between PCRE, JavaScript, and POSIX regex?
- PCRE (Perl-compatible) is the richest dialect — supports lookbehind, named groups, recursion. JavaScript regex is similar to PCRE but lacks variable-length lookbehind in older browsers. POSIX is older, mostly used in grep/sed/awk; lacks lookarounds entirely. Most online regex testers default to PCRE or JavaScript flavor.
- When should I NOT use regex?
- Parsing HTML, XML, or JSON — use proper parsers. Matching balanced brackets/parens — regex can't count. Validating email addresses for delivery — use proper email validation libraries that check MX records. The 'if it can be parsed' rule: if the language has a defined grammar, use a parser, not regex.
- Why is .* so slow on long input?
- It's catastrophic backtracking. Greedy quantifiers like .* try every possible match before giving up. On long inputs with nested quantifiers (e.g., (a+)+), regex can take exponential time. Solutions: use atomic groups (?>...), possessive quantifiers (*+, ++), or anchor with lookarounds. Re-write to avoid nested optionality.
- How do I match a literal special character?
- Escape it with a backslash. Special chars in regex: . * + ? ^ $ ( ) [ ] { } | \ /. To match a literal dot: \. To match a literal backslash: \\. Inside character classes [] most special chars lose meaning except - ] \ ^. So [.] matches a literal dot without escaping needed inside the class.
เครื่องมือที่เกี่ยวข้อง
- JSON Formatter, Validator & Repair Tool
Format, minify, validate, and repair JSON instantly in your browser. Sort keys alphabetically, auto-format on paste, download as file, escape/unescape strings — free, no sign-up, 100% client-side.
- ตัวสร้าง QR Code
สร้าง QR code สำหรับ URL ข้อความ Wi-Fi และอื่นๆ ดาวน์โหลดเป็น PNG
- ตัวสร้างรหัสผ่าน
สร้างรหัสผ่านสุ่มที่แข็งแกร่งด้วยความยาวและชุดอักขระที่กำหนดเอง
- Base64 เข้ารหัส / ถอดรหัส
เข้ารหัสข้อความเป็น Base64 หรือถอดรหัสกลับเป็นข้อความธรรมดา
- URL เข้ารหัส / ถอดรหัส
เข้ารหัสหรือถอดรหัส URL และ query strings ด้วย percent-encoding