Character sets

In the following table, you can find the patterns for character sets, which tell the Regex to match only one character out of several characters.

Pattern

Description

Example

[xyz]

This matches any one character enclosed in the character set. You can use a hyphen to denote the range. For example, /[a-z]/ matches any letter in the alphabet and matches /[0-9]/ to any single digit.

/[ao]/ matches "a" in "bar"

[^xyz]

This matches any one character, which is not enclosed in the character set.

/[^ao]/ matches "b" in "bar"

Get JavaScript Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.