Stax
Tools

ทดสอบ Regex

ทดสอบ regular expressions ด้วย live match highlighting และ capture groups

//g

วิธีใช้ regex tester

Pattern regex ทั่วไป

JavaScript regex flavour

เครื่องทดสอบนี้ใช้ RegExp engine ในตัวของ JavaScript — เดียวกับที่เบราว์เซอร์และ Node.js ของคุณใช้ Pattern ที่ใช้งานได้ที่นี่จะใช้งานได้ใน JavaScript code โดยตรง โปรดทราบว่า JavaScript ไม่รองรับ lookbehind ในเบราว์เซอร์รุ่นเก่า named capture groups ต้องการ ES2018+ และ flag s (dotall) ต้องการ ES2018+

นักพัฒนา backend ทดสอบ pattern การ validate form สำหรับที่อยู่อีเมล หมายเลขโทรศัพท์ และรหัส PIN ก่อนฝังไว้ใน server-side code วิศวกร frontend สร้าง prototype ตรรกะการ sanitize input โดยตรงในเบราว์เซอร์โดยไม่ต้องสลับไปยัง IDE นักวิจัยด้านความปลอดภัยใช้เพื่อวิเคราะห์ log pattern และดึง IP addresses, user agents หรือรหัสข้อผิดพลาดจาก log string ขนาดใหญ่ วิศวกรข้อมูลเขียน regex pattern เพื่อแยกวิเคราะห์ข้อความกึ่งโครงสร้างจาก CSV หรือ API responses ก่อนโหลดเข้าฐานข้อมูล นักเรียนที่เรียน regular expressions ใช้เพื่อทดลองกับ pattern จาก string จริงและเห็นทันทีว่า flag เช่น i และ m เปลี่ยนพฤติกรรมอย่างไร

JavaScript รองรับทั้ง positive และ negative lookahead ((?=...){" "} และ (?!...)) และ positive และ negative lookbehind ((?<=...) และ (?<!...)). Lookbehind ต้องการ ES2018 หรือใหม่กว่า แต่รองรับในเบราว์เซอร์สมัยใหม่ทั้งหมด ตัวอย่าง: (?<=₹)\d+ จับคู่ตัวเลขเฉพาะเมื่ออยู่หลังสัญลักษณ์ ₹ — มีประโยชน์สำหรับการดึงจำนวนเงินจากข้อความทางการเงินโดยไม่จับสัญลักษณ์สกุลเงิน ทดสอบ lookaround pattern ที่นี่เพื่อยืนยันว่าทำงานตามที่คาดก่อนนำไปใช้ใน production

เริ่มต้นด้วย pattern ที่ง่ายที่สุดที่เป็นไปได้และค่อยๆ สร้างขึ้น หาก pattern ไม่ match ให้ตรวจสอบ: ตัวอักษรพิเศษที่ไม่ได้ escape (. ที่ไม่ได้ escape จะ match ตัวอักษรใดๆ — ใช้ \. สำหรับจุดตัวอักษร); การขาด flag m เมื่อยึดกับขอบบรรทัดด้วย ^ และ $; greedy vs lazy quantifiers ที่กินมากกว่าที่คาด ใช้ named capture groups ((?<year>\d{4})) เพื่อทำให้ pattern ซับซ้อนอธิบายตัวเองได้

  1. ป้อน regular expression pattern ของคุณในช่องด้านบน (โดยไม่ต้องมี slash ล้อมรอบ)
  2. สลับ flag ตามต้องการ: i สำหรับ case insensitive, m สำหรับ multiline, s สำหรับ dotall
  3. วาง test string ของคุณในพื้นที่ข้อความด้านล่าง
  4. การจับคู่จะถูก highlight เป็นสีเหลืองทันที รายการ match แสดงการจับคู่แต่ละครั้ง ตำแหน่ง และ capture groups ใดๆ
  • \d+ — ตัวเลขหนึ่งตัวหรือมากกว่า
  • [a-zA-Z]+ — ตัวอักษรหนึ่งตัวหรือมากกว่า
  • \b\w+\b — คำทั้งคำ
  • ^.+$ — บรรทัดที่ไม่ว่างทั้งบรรทัด (ด้วย flag m)
  • [\w.+-]+@[\w-]+\.[a-z]+ — email พื้นฐาน
  • https?://[^\s]+ — URLs
  • \d{3}-\d{4} — รูปแบบโทรศัพท์

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

What is a regular expression?
A regular expression (regex) is a pattern that describes a set of strings. It's used to search, match, extract, validate, and replace text. For example, /\d+/ matches one or more digits, and /^[a-z]+$/i matches strings containing only letters.
What flags are supported?
This tester supports three flags: i (case insensitive — A matches a), m (multiline — ^ and $ match line boundaries, not just string boundaries), and s (dotall — . matches newlines too). The g (global) flag is always on so all matches are found.
How do I match a literal dot or parenthesis?
Escape it with a backslash: \. matches a literal dot, \( matches a literal opening parenthesis. In a regex pattern, characters like . * + ? [ ] { } ( ) ^ $ | \ have special meaning and must be escaped if you want them literally.
How do capture groups work?
Wrap part of your pattern in parentheses to create a capture group. For example, /(\d{4})-(\d{2})-(\d{2})/ matches a date and captures year, month, and day as separate groups. The match list shows captured groups in the Groups column.
What's the difference between greedy and lazy matching?
Greedy quantifiers (*, +, ?) match as much as possible. Lazy quantifiers (*?, +?, ??) match as little as possible. For example, /<.+>/ on '<b>text</b>' matches the whole thing. /<.+?>/ matches '<b>' and '</b>' separately.

เครื่องมือที่เกี่ยวข้อง