JavaScript Notes
Master JavaScript regex character classes: square bracket syntax, negated classes, ranges [a-z], shorthand classes \\d \\w \\s, and real-world pattern examples with interview Q&A.
Character classes let you match any single character from a defined set. Instead of matching one specific character, you define a "menu" of acceptable characters using square brackets [...]. They are the building blocks of almost every regex pattern.
📊 Character Class Visual Reference
💻 Example 1 — Basic Character Classes
true false [ 'e', 'o', 'o' ] false true true true false
💻 Example 2 — Negated Classes [^...]
HelloWorld 123 HelloWorld123 false true
💻 Example 3 — Combined Ranges and Classes
true false true true true false false true false
💻 Example 4 — Shorthand vs Explicit Classes
[ '9876543210', '95', '5' ] [ 'Hello', 'Riya', 'Phone', '9876543210', 'Score', '95', '5' ] 7 123456 Hello World
💻 Example 5 — Character Classes Inside Complex Patterns
Weak (2/5) Strong (4/5) Very Strong (5/5)
📋 Character Class Quick Reference
| Class | Meaning | Equivalent |
|---|---|---|
[abc] | a, b, or c | — |
[^abc] | NOT a, b, or c | — |
[a-z] | Any lowercase letter | — |
[A-Z] | Any uppercase letter | — |
[0-9] | Any digit | \d |
[a-zA-Z0-9_] | Word character | \w |
[ \t\n\r] | Whitespace | \s |
[^0-9] | Not a digit | \D |
[^a-zA-Z0-9_] | Not a word char | \W |
⚠️ Common Mistakes
❌ Mistake 1 — Hyphen Not at Start or End (Treated as Range)
❌ Mistake 2 — Dot Inside [] Is Literal
true false true
❌ Mistake 3 — Using [...] When You Need (...)
true false false
🎯 Key Takeaways
[abc]matches any one character from the set[^abc]matches any one character NOT in the set- Ranges:
[a-z],[A-Z],[0-9]— use hyphen between start and end - Shorthand classes:
\d,\w,\sand their negations\D,\W,\S - Inside
[], most special characters lose their meaning (.is literal dot) - Hyphen
-inside[]must be at start or end to be treated as a literal hyphen
❓ Interview Questions
Q1. What is a character class in regex? > A character class [...] matches any single character from the set inside the brackets. [aeiou] matches any one vowel. [a-z] matches any lowercase letter. The key point: it always matches exactly ONE character.
Q2. What does [^abc] mean? > The caret ^ inside brackets negates the class — it matches any single character that is NOT in the set. [^0-9] matches any non-digit character.
Q3. What is the difference between . and [.]? > Outside brackets, . matches any character except newline. Inside brackets [.], the dot loses its special meaning and matches a literal dot character only.
Q4. How is \d different from [0-9]? > For ASCII text they are equivalent. However, \d with the u flag in Unicode mode can match digits from other scripts (Arabic numerals, etc.), while [0-9] strictly matches only the digits 0-9.
Q5. How do you include a literal hyphen in a character class? > Put it at the beginning [-abc] or end [abc-] of the class. In the middle, a hyphen defines a range — [a-c] means a, b, or c. [-] or [a\-z] (escaped) also works.
Q6. Can you combine ranges and individual characters in one class? > Yes — [a-zA-Z0-9_] combines three ranges and one character. You can mix freely: [a-f0-9!@#] matches hex digits plus three symbols.
Q7. What does \w match? > \w is shorthand for [a-zA-Z0-9_] — letters (upper and lower), digits, and underscore. It does NOT match spaces, hyphens, or other special characters.
Q8. How would you match any character that is NOT a whitespace? > Use \S (capital S) — shorthand for [^\s]. Or explicitly: [^ \t\n\r\f\v]. Useful for extracting words from text.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Regex Character Classes – [abc], [^abc], Ranges & Shortcuts.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, regex, character, classes
Related JavaScript Master Course Topics