Execute Program

Regular Expressions: Character Classes

Welcome to the Character Classes lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • Regexes have many special cases to solve common problems. For example, we often write regexes to process source code. Most code contains identifiers: variable names, function names, etc. In most languages, identifiers can have letters, numbers, and underscores.

  • We could write [A-Za-z0-9_] to mean "letters, numbers, and underscores." But that's verbose, and regexes are meant to be terse. Instead, we can use the character class \w. The "w" in \w stands for "word", which is another name for an identifier. This can be tricky: "word" has a special meaning in programming!

  • >
    /\w/.test('a');
    Result:
    truePass Icon
  • >
    /\w/.test('+');
    Result:
    falsePass Icon
  • >
    /\w/.test('F');
    Result:
    truePass Icon
  • >
    /\w/.test('_');
    Result:
    truePass Icon
  • >
    /a\wc/.test('abc');
    Result:
    truePass Icon
  • >
    /a\wc/.test('a-c');
    Result:
    falsePass Icon
  • >
    /^\w$/.test('aaa');
    Result:
    falsePass Icon
  • We can match entire identifiers by combining \w with + and boundaries.

  • >
    /^\w+$/.test('my_variable');
    Result:
    truePass Icon
  • >
    /^\w+$/.test('1+1');
    Result:
    falsePass Icon
  • >
    /^\w+$/.test('while');
    Result:
    truePass Icon
  • >
    /^\w+$/.test('while (true)');
    Result:
    falsePass Icon
  • As with any character class, upper-casing the class negates it. (\W is the opposite of \w.)

  • >
    /\w/.test('c');
    Result:
    truePass Icon
  • >
    /\W/.test('c');
    Result:
    falsePass Icon
  • >
    /\w/.test('!');
    Result:
    falsePass Icon
  • >
    /\W/.test('!');
    Result:
    truePass Icon