Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท Character Classes and Quantifiers

Character Classes

Master regex character classes. Match one character from a set with [abc], use ranges like [a-z], negate with [^...], and learn the special rules for -, ^, ] and \ inside brackets.

A character class matches exactly one character out of a set you define. It is written with square brackets: [aeiou] matches one vowel. Classes are the most common building block after literals, and they replace long alternations like (a|e|i|o|u).

Listing characters

Inside the brackets, list the characters you accept. Order does not matter and duplicates are ignored.

Node.js A set of characters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Remember: [aeiou] is one character. To match several, add a quantifier: [aeiou]+ is one or more vowels in a row.

Ranges

A hyphen between two characters defines a range, based on character codes: [a-z], [0-9], [A-Fa-f]. You can combine several ranges and single characters in one class.

Node.js Ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Ranges follow character codes, not the alphabet of your language. [a-z] does not contain รฉ, รฑ or รŸ, and [A-z] (a common typo) accidentally includes [ \ ] ^ _ and the backtick, which sit between Z and a in ASCII.

Negated classes: [^...]

A caret right after the opening bracket negates the class: [^0-9] matches one character that is not a digit. A negated class also matches line breaks.

Node.js Everything except...

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The pattern "[^"]*" is a very common idiom: an opening quote, any number of non-quote characters, a closing quote. It is more precise and faster than ".*".

Special characters inside a class

Inside brackets most metacharacters lose their special meaning. The few that remain special:

Character Where it is special How to use it literally
] ends the class \]
\ starts an escape \\
^ at the very start: negation not first, or \^
- between two characters: range first, last or \-
Node.js Dots, stars and dashes in a class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Note the last line: inside a class, \b means the backspace character, not a word boundary (a boundary is a position, and a class matches characters).

Shorthand and classes together

You can use shorthand classes such as \d and \s inside brackets (see the next lesson) and mix them with ranges and literals.

Node.js Combining pieces

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The last pattern prints a match that starts with a space: [\d ]+ includes the space after tel: as well. That is a good reminder to include only what you mean in a class.

Case-insensitivity and classes

With the i flag, [a-z] also matches uppercase letters, so you do not need [a-zA-Z].

Node.js i flag with ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many characters does [abc] match at once?

    A class matches one character from its set. Add a quantifier to repeat it.
  2. 2. What does [^0-9] match?

    A caret just after the opening bracket negates the class.
  3. 3. Where can a hyphen sit in a class to be a literal hyphen without escaping?

    At the start or end it cannot form a range, so it is read as a plain hyphen. Escaping it with \- works anywhere.
  4. 4. Why is [A-z] a bad choice for letters?

    ASCII has six symbols between Z and a, and the range covers them too.

Summary

  • [...] matches exactly one character from a set; add a quantifier to repeat it.
  • Ranges (a-z) follow character codes; they do not cover accented letters.
  • [^...] negates the class and also matches line breaks.
  • Inside a class, only ], \, a leading ^ and a middle - are special.

Next: Shorthand Classes and Negation