ข้อความ
Regex Tester / Find & Replace
ทดสอบ regular expression กับข้อความจริง: ไฮไลต์ที่ตรงกันและกลุ่มที่จับได้ถูกวิเคราะห์ หรือแทนที่สิ่งที่ตรงกันด้วยข้อความใหม่
A regular expression (regex) is a compact language for describing patterns in text — for searching, validating a format, or replacing matches. This tool highlights matches in real text as you type the pattern, breaks out capture groups, and supports a find-and-replace mode.
How to use it
- Search: enter a pattern and some test text — every match is highlighted and capture groups are broken out separately.
- Replace: switch to Replace mode, provide a replacement template (referencing groups with $1, $2), and see the result instantly.
- Flags (g, i, m, s) toggle independently and immediately affect how many matches are found and how.
Common uses
- Building and debugging a regex before pasting it into code — you see the result without running the program.
- Bulk find-and-replace against a pattern (e.g. reformatting phone numbers or dates).
- Validating an input format (email, card number, slug) against real examples.
Things to keep in mind
Greedy quantifiers (*, +) grab as many characters as possible — add ? for the minimal match instead (e.g. .*? instead of .*).
Syntax varies slightly between languages — an expression that works here (a JavaScript engine) may need adjusting for PCRE, .NET, or Python.
บทความเกี่ยวกับเครื่องมือนี้: Regular Expression: พื้นฐานและกับดักของการจับคู่แบบโลภ
คำถามที่พบบ่อย
ใช้เอนจิ้น regex แบบใด?
เครื่องมือนี้ใช้เอนจิ้น regex ของ JavaScript ไม่ใช่ PCRE ดังนั้นไวยากรณ์เฉพาะของ PCRE บางอย่าง (เช่น assertion แบบเรียกซ้ำหรือโหมดจับคู่แบบ possessive บางอย่าง) จะไม่ทำงานที่นี่; รองรับแฟล็กทั่วไป เช่น g, i, m, s และ u
ตัวระบุจำนวนแบบ greedy กับ lazy ต่างกันอย่างไร และ Test กับ Replace ต่างกันอย่างไร?
ตัวระบุจำนวนแบบ greedy เช่น * หรือ + จะจับข้อความให้ได้มากที่สุด ในขณะที่เวอร์ชัน lazy (*?, +?) จะหยุดโดยเร็วที่สุด; สำหรับโหมด Test จะไฮไลต์ผลที่ตรงกันและแสดงกลุ่มจับคู่ (รวมถึงกลุ่มที่มีชื่อ) ส่วน Replace จะใช้ข้อความแทนที่ พร้อมการอ้างอิงเช่น $1 กับผลที่ตรงกันแต่ละรายการ
ข้อความและนิพจน์ของฉันถูกส่งไปยังเซิร์ฟเวอร์หรือไม่?
ไม่ การประเมินนิพจน์ปกติเกิดขึ้นทั้งหมดฝั่งไคลเอนต์ในเบราว์เซอร์ของคุณ ไม่มีอะไรถูกส่งออนไลน์
Catastrophic backtracking คืออะไร?
Quantifier ที่ซ้อนกันเช่น (a+)+ อาจทำให้เอนจิ้นต้องไล่ลองความเป็นไปได้จำนวนมหาศาลแบบ exponential กับข้อมูลนำเข้าบางแบบ — หน้าเว็บหรือสคริปต์อาจ "ค้าง" กับนิพจน์ที่ดูเรียบง่าย นี่คือช่องโหว่จริง (ReDoS) ดังนั้น quantifier ที่ซ้อนกันซับซ้อนควรทดสอบกับข้อความยาว ๆ ที่เกือบตรงกัน
ทำไมนิพจน์ของฉันทำงานในภาษาโปรแกรมหนึ่ง แต่ไม่ทำงานในอีกภาษา?
ไวยากรณ์ regex ไม่ได้เป็นมาตรฐานเดียวกันทั้งหมด: PCRE (PHP และอีกหลายภาษา), .NET, Python re และ JavaScript มีความแตกต่างเล็กน้อยในการรองรับบางโครงสร้าง (เช่น look-behind หรือกลุ่มที่มีชื่อ) — นิพจน์ที่เขียนสำหรับเอนจิ้นหนึ่งอาจไม่ทำงานเหมือนเดิมในอีกเอนจิ้นโดยไม่มีการแก้ไข