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

Character sets: `[abc]`

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

Square brackets define a set of characters allowed in one position.

What you'll learn in this lesson

  • Build custom character sets with square brackets [...] to express compact alternatives on a single character.
  • Distinguish the meta-characters that become literal inside [...] (., *, +, ?, (, )) from the 4 still special symbols (\, ], ^, -).
  • Place ] and - safely inside a custom class without syntax errors or unnecessary escapes.

Overview

When a predefined class like \d or \w isn't enough, you can define a custom character set with the square brackets [...]. Each character inside the squares represents an alternative: the entire class consumes exactly one character of the text if it belongs to the indicated set.

Imagine [aeiou] as a closed question with five valid options for a single position: if the current character is one of those five letters, the class succeeds and consumes one character, then the cursor advances.

Sections covered in this lesson

  1. 1Mental model: a set as a single choice menu
  2. 24 progressive examples step by step
  3. 3Guided practical exercises
  4. 4Quick check
Pro Lesson · Module 5 — Character classes

Unlock "Character sets: `[abc]`" and the full Regex Course curriculum

Square brackets define a set of characters allowed in one position.

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.