Skip to main content
Module 5 · Lesson 3 of 419/32 in the course~15 min
Module lessons (3/4)

Negation: `[^...]`

Lesson preview5 hands-on exercises readyRegex Course · Module 5 — Character classes

A negated class matches anything except the listed characters.

What you'll learn in this lesson

  • Reverse any character class by prefixing ^ as the first character inside the squares ([^...]).
  • Replace risky wildcard matches (".+?") with fast, backtracking-free negated classes ("([^"]+)").
  • Distinguish class negation ([^a] inside [...]) from string start anchor (^a outside [...]).

Overview

Placing a caret ^ as the first character inside the square brackets reverses the meaning of the class: [^aeiou] consumes exactly any character as long as it is not any of those listed.

While [...] functions as a whitelist ("accept only these"), [^...] functions as a blacklist ("accept everything except these"). It is a key technique for stopping a match in front of a delimiter (quote, comma, slash) without generating excessive backtracking.

Sections covered in this lesson

  1. 1Mental model: the exclusion list
  2. 24 progressive examples step by step
  3. 3Guided practical exercises
  4. 4Quick check
Pro Lesson · Module 5 — Character classes

Unlock "Negation: `[^...]`" and the full Regex Course curriculum

A negated class matches anything except the listed characters.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Regex Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.