Negated character class
Within a character class, the caret (^) is used to negate the class. The character class [aeiou] matches vowels, negating it with the caret [^aeiou], which matches any character except a vowel (including spaces, punctuation, tabs, and everything).
As with the hyphen, the caret is only effective if it is in the right position. In this case, it only negates the class if it is the first character. Elsewhere in the class, it is a literal character.
A negated character class is sometimes the fastest way to tackle a problem. If the list of expected characters is small, negating that list is a quick way to perform a match.
In the following example, the negated character class is used with the replace operator to fix a problem: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access