liveBook · Manning

Character class

As explained in Secrets of the JavaScript Ninja, Third Edition

A character class is a regular-expression construct that matches one character from a specified set or range.

Need to match one character without spelling out every possibility? A character class puts the choices inside square brackets. `[abc]` matches one `a`, `b`, or `c`; `[a-m]` matches one character from `a` through `m`, inclusive. A class is one regex term, so `[a-m]+` matches a sequence of those characters. section 12.2.2 section 12.7

Three details prevent common mistakes:

  • Put `^` immediately after `[` to exclude the listed characters: `[^abc]` matches one character other than `a`, `b`, or `c`. Outside a class, `^` can instead anchor a match to the start of the string. section 12.2.2
  • A dash can specify an inclusive range. To include a literal dash alongside `a` through `m`, put it first: `[-a-m]`. section 12.2.2
  • Shorthands can stand in for familiar sets: `\d` corresponds to `[0-9]`, and `\w` to `[A-Za-z0-9_]`. Their uppercase counterparts negate those sets. `\b` is different: it marks a word boundary, not a character to consume. section 12.2.2 section 12.4.3

Extract tokens while leaving separators out

`/([^\s,]+) of ([^\s,]+)/g` uses `[^\s,]` to match one character that is neither whitespace nor a comma. The `+` extends that choice into a sequence; parentheses capture each sequence. Used with `matchAll`, the pattern extracts names and clans from a comma-separated roster. The class alone does not match a whole name—the repetition operator does that work. section 12.4.2

Match a letter after a hyphen

`/-([a-z])/g` finds a hyphen followed by a lowercase letter. With `replace` and a function that uppercases the captured letter, it changes `shadow-clone-jutsu` to `shadowCloneJutsu`. Here the hyphen is outside the brackets, so it is a literal hyphen rather than range syntax. section 12.5

Choose a class for one character from a set, a negated class for one character outside it, and a repetition operator when the choice must continue. Add groups, boundaries, or flags when the surrounding pattern needs extraction, position checks, or different matching behavior. section 12.2.2 section 12.4.3 section 12.7

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains bracket syntax, negation, ranges, shorthands, and repetition through patterns; does not examine character-class engine internals.
Examples
Uses excluded separators in a roster, lowercase letters in a doubled-word pattern, and hyphenated text converted to camel case.
What it assumes
Introduces classes alongside regex terms and operators while using JavaScript strings and methods in its examples.
What you can do after
Apply it via JavaScript regular expressions to specify single-character choices and combine them with repetition, captures, or replacement.
How it connects
Connects character classes to quantifiers, capturing groups, word boundaries, regex flags, global matching, and string replacement.

Also explained in

Compare how 10 books explain character class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.