JavaScript Regex Character Classes and Escapes
Quick answer
A character class matches one member of a set at the current position. Escapes can denote special classes or literal punctuation.
How it works
[abc] matches a, b or c; [^abc] matches a character outside that set. Ranges like [0-9] are inclusive. \d matches ASCII digits, \w usually matches ASCII word characters (with some Unicode-aware case-insensitive extensions), and \s matches whitespace. Dot matches a character except line terminators unless the s flag is set; Unicode-aware mode affects character width.
console.log(/[A-F]/.test("C"), /[^0-9]/.test("7"));Expected output in Node.js:
true falseC is in the inclusive range; 7 is excluded by the negated class.
console.log(/\d/.test("۵"), /\./.test("a.b"));Expected output in Node.js:
false trueThe Persian digit is not ASCII; the escaped dot matches punctuation.
Common mistakes
\d is not a match for every Unicode decimal digit. A literal dot outside a class needs escaping.
Practice
Find an ASCII digit in a label and compare it with a Persian digit.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Regex Character Classes and Escapes — Knowledge Check
Apply the taught concept and predict the output.
What does a caret immediately after the opening bracket mean in [^abc]?
Which class matches ASCII digits?
How do you match a literal dot outside a class?
What is the exact console output in JavaScript Regex Character Classes and Escapes example 1?
console.log(/[A-F]/.test("C"), /[^0-9]/.test("7"));What is the exact console output in JavaScript Regex Character Classes and Escapes example 2?
console.log(/\d/.test("۵"), /\./.test("a.b"));