ใน JavaScript Regular Expressions Special Characters (Metacharacters) ใช้ควบคุม จำนวนครั้ง (Quantifiers), ตำแหน่ง (Anchors), การจับคู่กลุ่ม (Grouping), และ Logic อื่นๆ นี่คือตัวสำคัญ:
Quantifiers (ตัวกำหนดจำนวน)
| Pattern | ชื่อ | ความหมาย | ตัวอย่าง Match |
X+ | One or more (+) | X ติดกัน 1 ตัวขึ้นไป | "X", "XX", "XXX" |
X* | Zero or more (*) | X ติดกัน 0 ตัวขึ้นไป | "", "X", "XX" |
X? | Zero or one (?) | X ติดกัน 0 หรือ 1 ตัว | "", "X" |
X{n} | Exactly n | X ติดกัน n ตัวพอดี | n=3: "XXX" |
X{n,} | n or more | X ติดกัน n ตัวขึ้นไป | n=2: "XX", "XXX" |
X{n,m} | Between n and m | X ติดกัน n ถึง m ตัว | n=2,m=4: "XX", "XXX", "XXXX" |
Anchors (ตัวกำหนดตำแหน่ง)
| Pattern | ชื่อ | ความหมาย |
^X | Start Anchor | X อยู่ต้น String (หรือต้นบรรทัดกับ flag m) |
X$ | End Anchor | X อยู่ท้าย String (หรือท้ายบรรทัดกับ flag m) |
\b | Word Boundary | ขอบเขตระหว่าง Word Char (\w) และ Non-Word Char (\W) |
\B | Non-Word Boundary | ไม่ใช่ Word Boundary |
Special Characters อื่นๆ
| Char | ชื่อ | ความหมาย |
. | Dot | จับคู่ตัวอักษรใดๆ ยกเว้น Newline (ใช้ s flag / [\s\S] สำหรับรวม Newline) |
\ | Escape | Escape ตัวอักษรพิเศษ: \. = จุดจริง, \* = ดาวจริง, \\ = Backslash จริง |
| | Alternation (OR) | จับคู่ Pattern ด้านซ้าย หรือ ด้านขวา: cat|dog |
() | Grouping / Capturing | จับกลุ่ม: (abc)+ = "abcabc", ใช้ \1, $1 อ้างอิงกลุ่มที่จับได้ |
(?:) | Non-capturing Group | จับกลุ่มแต่ ไม่ Capture (ประหยัด Memory, เร็วกว่า) |
(?=) | Positive Lookahead | Assert ว่าต่อจากนี้มี Pattern นี้ (ไม่ Consume) |
(?!) | Negative Lookahead | Assert ว่าต่อจากนี้ ไม่มี Pattern นี้ |
(?<=) | Positive Lookbehind | Assert ว่าก่อนหน้ามี Pattern นี้ (ES2018+) |
(? | Negative Lookbehind | Assert ว่าก่อนหน้า ไม่มี Pattern นี้ (ES2018+) |
Character Classes (ในวงเล็บ []) - Special Characters ภายใน
^ ตัวแรก = Negation: [^abc]
- ระหว่าง = Range: [a-z]
] ตัวแรก (หลัง ^ ถ้ามี) = ตัวอักษร ] จริง: []]
\ Escape: [\$\.\*\+]
Flags (Modifiers)
| Flag | ชื่อ | ผลกระทบ |
g | Global | หาทุก Occurrence (ไม่หยุดที่เจอแรก) |
i | Case Insensitive | ไม่สนใจพิมพ์ใหญ่/เล็ก |
m | Multiline | ^ และ $ จับคู่ต้น/ท้าย บรรทัด (แทน String) |
s | DotAll (ES2018) | . จับคู่ Newline ด้วย |
u | Unicode (ES2015) | รองรับ Unicode Property Escapes (\p{...}), Surrogate Pairs ถูกต้อง |
y | Sticky (ES2015) | จับคู่เฉพาะตำแหน่ง lastIndex (ไม่ขยับ) |
ตัวอย่างการใช้งาน
const str = "Welcome to our website! Welcome to Tutorialspoint!";
// 1. Wel* = "We" + "l" 0 หรือมากครั้ง -> "We", "Wel", "Well", "Welll"...
const reg1 = /Wel*/g;
console.log(str.match(reg1)); // ["Wel", "Wel"] (จาก "Welcome" x2)
// 2. Wel+ = "We" + "l" 1 หรือมากครั้ง -> "Wel", "Well", "Welll"...
const reg2 = /Wel+/g;
console.log(str.match(reg2)); // ["Wel", "Wel"]
// 3. ^Welcome = เริ่มต้นด้วย Welcome
console.log(/^Welcome/.test(str)); // true
// 4. point!$ = จบด้วย point!
console.log(/point!$/.test(str)); // true
// 5. \bJava\b = คำว่า "Java" แยกคำชัดเจน
console.log(/\bJava\b/.test("JavaScript")); // false
console.log(/\bJava\b/.test("Java is great")); // true
// 6. Lookahead: รหัสผ่านต้องมีตัวเลข (?=.*\d) และตัวพิมพ์ใหญ่ (?=.*[A-Z])
const pwdRegex = /^(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,}$/;
console.log(pwdRegex.test("Pass1234")); // true
console.log(pwdRegex.test("password")); // false (ไม่มีตัวใหญ่, ตัวเลข)
สรุป
Special Characters ใน Regex ควบคุม Quantifiers (+, *, ?, {n,m}), Anchors (^, $, \b), Grouping ((), (?:), Lookahead/behind), Alternation (|), Escape (\) และ Flags (g, i, m, s, u, y) การเข้าใจแต่ละตัวช่วยเขียน Pattern ที่แม่นยำและมีประสิทธิภาพ