YouTip LogoYouTip

Regex Character Classes

Introduction

Character classes let you define a set of characters to match. They use square brackets [] to specify which characters are acceptable at a position.

Basic Character Classes

         Match a, b, or c
         Match any lowercase letter
         Match any uppercase letter
         Match any digit
   Match any alphanumeric character

# Examples
//     matches vowels
/{3}/    matches 3-digit numbers
//  matches capitalized words

Negated Character Classes

[^abc]        Match anything except a, b, c
[^0-9]        Match any non-digit
[^s]         Match any non-whitespace

# Examples
/[^aeiou]/    matches consonants
/[^@s]+@/    matches email local part

Shorthand Classes

d                       Digit
D            [^0-9]          Non-digit
w                Word character
W            [^w]           Non-word character
s                 Whitespace
S            [^s]           Non-whitespace

Practical Examples

# Match hex color codes
/#{6}/

# Match filenames
/+.w{2,4}/

# Match mixed case words
/ello orld/

Summary

Character classes define what characters can match at a position. Use brackets for custom classes, ^ for negation, and shorthand like d for common patterns.

← Regex Quantifiers and AnchorsRegex Tutorial - Getting Start β†’