Character classes

suggest change

A character class matches one character from a set.

You can list the characters explicitly:

/[abc]/ # 'a' or 'b' or 'c'

Or use a range:

/[a-z]/ # from 'a' to 'z'

You can combine ranges and single characters:

/[a-cz]/ # 'a' or 'b' or 'c' or 'z'

A leading dash (-) is treated as a character:

/[-a-c]/ # '-' or 'a' or 'b' or 'c'

A class is negated by a leading ^:

/[^a-c]/ # Not 'a', 'b' or 'c'

There are shortcuts for common classes, plus anchors and other metacharacters:

^  # Start of line
$  # End of line
\A # Start of string
\Z # End of string, excluding any new line at the end of string
\z # End of string
.  # Any single character
\s # Any whitespace character
\S # Any non-whitespace character
\d # Any digit
\D # Any non-digit
\w # Any word character (letter, number, underscore)
\W # Any non-word character
\b # Any word boundary

\n is a newline.

To escape a reserved character, such as / or [], use a backslash:

\\ # => \
\[\] # => []

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents