นักพัฒนา

ทดสอบและดึงข้อมูลด้วย Regex

ทดสอบ regex แบบเรียลไทม์ ไฮไลต์ผลลัพธ์ ดู capture groups และสร้าง code snippet

พื้นที่ทำงาน Regex
จำนวนที่ตรง: 2
ผลที่ไฮไลต์
product-120
order-42
invalid
กลุ่มที่จับได้
#ข้อความที่ตรงทั้งหมดกลุ่ม
1product-120g1: product | g2: 120
2order-42g1: order | g2: 42
อ้างอิง

สรุปไวยากรณ์ Regex

สัญลักษณ์ความหมาย
.Any character except newline
\dDigit (0-9)
\wWord character
\sWhitespace
^ / $Start / end of line
[abc]Any character in class
[^abc]Any character not in class
( ... )Capture group
(?: ... )Non-capturing group
a|bAlternation
* + ?Quantifiers
{n,m}Range quantifier

ตัวอย่างโค้ด

const regex = new RegExp('(\\w+)-(\\d+)', 'g');
const text = 'product-120
order-42
invalid';
const matches = [...text.matchAll(regex.global ? regex : new RegExp(regex.source, regex.flags + 'g'))];
console.log(matches);

พบปัญหาหรือมีไอเดีย? แจ้งปัญหา

Share

คุณอาจชอบสิ่งนี้ด้วย