TL
Tool Lab
💰Donate
💰Donate

อ้างอิง Regex

คู่มืออ้างอิงด่วนสำหรับไวยากรณ์นิพจน์ปกติ: คลาสอักขระ, จุดยึด, ตัวระบุปริมาณ, กลุ่ม, แฟล็กและรูปแบบทั่วไป

คลาสอักขระ

.Any character except newline
\wWord character [a-zA-Z0-9_]
\WNon-word character
\dDigit [0-9]
\DNon-digit
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Character class — a, b, or c
[^abc]Negated class — not a, b, or c
[a-z]Range — any lowercase letter
[a-zA-Z0-9]Alphanumeric characters

จุดยึด

^Start of string (or line with multiline)
$End of string (or line with multiline)
\bWord boundary
\BNon-word boundary
\AStart of string (not multiline)
\ZEnd of string (not multiline)

ตัวระบุปริมาณ

*Zero or more (greedy)
+One or more (greedy)
?Zero or one (greedy)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Zero or more (lazy/non-greedy)
+?One or more (lazy/non-greedy)
??Zero or one (lazy/non-greedy)

กลุ่มและการสลับ

(abc)Capture group
(?:abc)Non-capturing group
(?P<name>abc)Named capture group
a|bAlternation — a or b
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Escape และอักขระพิเศษ

\nNewline
\tTab
\rCarriage return
\0Null character
\uXXXXUnicode character (hex)
\.Literal dot (escaped)
\\Literal backslash

แฟล็ก

gGlobal — find all matches
iCase-insensitive
mMultiline — ^ and $ match line start/end
sDotall — . matches newline
uUnicode — treat pattern as Unicode
ySticky — match from lastIndex

รูปแบบทั่วไป

^[\w.-]+@[\w.-]+\.\w{2,}$Email address
https?://[\w./%-]+URL (basic)
^\d{1,3}(\.\d{1,3}){3}$IPv4 address
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Hex color
^\d{4}-\d{2}-\d{2}$ISO date (YYYY-MM-DD)
^[+]?[\d\s()-]{7,15}$Phone number (basic)
^(?=.*[A-Z])(?=.*\d).{8,}$Password: 8+ chars, 1 uppercase, 1 digit

เกี่ยวกับเครื่องมือนี้

นิพจน์ปกติ (regex) เป็นภาษาที่ทรงพลังสำหรับอธิบายรูปแบบในสตริง เอกสารอ้างอิงนี้ครอบคลุมไวยากรณ์และรูปแบบที่ใช้บ่อยที่สุด

ส่วนต่างๆ จัดระเบียบตามหมวดหมู่ ใช้ช่องค้นหาเพื่อกรองรูปแบบด้วยคำสำคัญ

วิธีใช้งาน

  1. เรียกดูส่วนต่างๆ เพื่อค้นหาไวยากรณ์ที่ต้องการ
  2. ใช้การค้นหาเพื่อกรองด้วยคำสำคัญ
  3. คลิกที่รูปแบบใดก็ได้เพื่อคัดลอกไปยังคลิปบอร์ด
  4. ทดสอบรูปแบบในเครื่องมือ Regex Tester

กรณีการใช้งาน

มีประโยชน์สำหรับการตรวจสอบแบบฟอร์ม การแยกวิเคราะห์สตริง การวิเคราะห์บันทึก การค้นหาและแทนที่ และงานการเขียนโปรแกรมอื่นๆ

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

  • ไวยากรณ์ regex ทำงานได้ในทุกภาษาหรือไม่?ภาษาส่วนใหญ่รองรับ regex แต่มีความแตกต่างเล็กน้อยในไวยากรณ์ ภาษาหลักเช่น JavaScript, Python, Java ส่วนใหญ่ปฏิบัติตามไวยากรณ์ที่เข้ากันได้กับ PCRE
  • จะทำให้รูปแบบไม่คำนึงถึงตัวพิมพ์ใหญ่/เล็กได้อย่างไร?เพิ่มแฟล็ก i หลังรูปแบบ ตัวอย่าง: /pattern/i
  • ความแตกต่างระหว่างตัวระบุปริมาณโลภและขี้เกียจคืออะไร?ตัวระบุปริมาณโลภ (*, +, ?) จับคู่มากที่สุดเท่าที่เป็นไปได้ ส่วนขี้เกียจ (*?, +?, ??) จับคู่น้อยที่สุดเท่าที่เป็นไปได้