Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

บทบาทของ Special Characters ใน JavaScript Regular Expressions

ใน 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 nX ติดกัน n ตัวพอดีn=3: "XXX"
X{n,}n or moreX ติดกัน n ตัวขึ้นไปn=2: "XX", "XXX"
X{n,m}Between n and mX ติดกัน n ถึง m ตัวn=2,m=4: "XX", "XXX", "XXXX"

Anchors (ตัวกำหนดตำแหน่ง)

Patternชื่อความหมาย
^XStart AnchorX อยู่ต้น String (หรือต้นบรรทัดกับ flag m)
X$End AnchorX อยู่ท้าย String (หรือท้ายบรรทัดกับ flag m)
\bWord Boundaryขอบเขตระหว่าง Word Char (\w) และ Non-Word Char (\W)
\BNon-Word Boundaryไม่ใช่ Word Boundary

Special Characters อื่นๆ

Charชื่อความหมาย
.Dotจับคู่ตัวอักษรใดๆ ยกเว้น Newline (ใช้ s flag / [\s\S] สำหรับรวม Newline)
\EscapeEscape ตัวอักษรพิเศษ: \. = จุดจริง, \* = ดาวจริง, \\ = Backslash จริง
|Alternation (OR)จับคู่ Pattern ด้านซ้าย หรือ ด้านขวา: cat|dog
()Grouping / Capturingจับกลุ่ม: (abc)+ = "abcabc", ใช้ \1, $1 อ้างอิงกลุ่มที่จับได้
(?:)Non-capturing Groupจับกลุ่มแต่ ไม่ Capture (ประหยัด Memory, เร็วกว่า)
(?=)Positive LookaheadAssert ว่าต่อจากนี้มี Pattern นี้ (ไม่ Consume)
(?!)Negative LookaheadAssert ว่าต่อจากนี้ ไม่มี Pattern นี้
(?<=)Positive LookbehindAssert ว่าก่อนหน้ามี Pattern นี้ (ES2018+)
(?Negative LookbehindAssert ว่าก่อนหน้า ไม่มี Pattern นี้ (ES2018+)

Character Classes (ในวงเล็บ []) - Special Characters ภายใน

  • ^ ตัวแรก = Negation: [^abc]
  • - ระหว่าง = Range: [a-z]
  • ] ตัวแรก (หลัง ^ ถ้ามี) = ตัวอักษร ] จริง: []]
  • \ Escape: [\$\.\*\+]

Flags (Modifiers)

Flagชื่อผลกระทบ
gGlobalหาทุก Occurrence (ไม่หยุดที่เจอแรก)
iCase Insensitiveไม่สนใจพิมพ์ใหญ่/เล็ก
mMultiline^ และ $ จับคู่ต้น/ท้าย บรรทัด (แทน String)
sDotAll (ES2018). จับคู่ Newline ด้วย
uUnicode (ES2015)รองรับ Unicode Property Escapes (\p{...}), Surrogate Pairs ถูกต้อง
ySticky (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 ที่แม่นยำและมีประสิทธิภาพ