Regex Character Class

Syntax for specifying character sets like [a-z], d, w. Defines the range of characters to match.

A regex character class defines a set of characters to match. There are two types: custom classes using square brackets [] and predefined shorthand classes like \d, \w, and \s. As a fundamental building block of regular expressions, character classes appear in virtually every pattern.

Custom character classes use [a-z] for lowercase letters, [0-9] for digits, and [a-zA-Z0-9] for alphanumeric characters. Placing ^ at the start creates a negated class: [^abc] matches any character except a, b, or c. Ranges are specified with hyphens, so [A-F0-9] concisely represents hexadecimal characters.

Predefined classes are shortcuts for common character sets. \d matches digits (equivalent to [0-9]), \w matches word characters ([a-zA-Z0-9_]), and \s matches whitespace (spaces, tabs, newlines, etc.). Uppercase versions negate them: \D matches non-digits, \W matches non-word characters.

Unicode-aware regex supports property escapes like \p{Script=Hiragana} to match characters from specific scripts or categories. In JavaScript, this is available since ES2018 when combined with the u flag. For processing Japanese text, \p{Script=Han} (kanji) and \p{Script=Katakana} (katakana) are particularly useful.

Inside character classes, most metacharacters are treated as literals, but ] (class end), \ (escape), ^ (negation at start), and - (range) retain special meaning. To match these literally, escape them or place them in positions where they lose their special meaning.

For character counting, character classes enable counting specific types of characters. For example, \p{Script=Han} counts kanji characters and \d counts digits individually, which is useful for text composition analysis.

Share this article