CHARACTER CLASSES

Negated sets

A caret as the first character flips a set to match everything you did not list.

Put a caret right after the opening bracket and the set flips. [^aeiou] matches any single character that is NOT one of a, e, i, o, u. In "cat" it matches c and t and skips the a. The caret means "anything except these".

Everything else about sets carries over: it still consumes exactly one character. [^0-9] matches one non-digit, so in "a1b2" it matches a and b and skips the digits.

Negated sets are the workhorse of real-world regex. "Everything up to the next quote" is [^"], repeated once you can say "repeated" - the quantifiers section adds that, and [^"] turns out to be the precise alternative to a lazy quantifier in greedy vs lazy.

The caret negates only as the FIRST character in the set. Anywhere else it is a plain character: [a^] matches a or a literal caret.

EXERCISE

Match every character that is not a digit.

/ /
a1b2
must match: "a" "b"
x9y
must match: "x" "y"
123
must match nothing