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\wstands for "word", which is another name for an identifier. This can be tricky: "word" has a special meaning in programming!>
/\w/.test('a');Result:
true
>
/\w/.test('+');Result:
false
>
/\w/.test('F');Result:
true
>
/\w/.test('_');Result:
true
>
/a\wc/.test('abc');Result:
true
>
/a\wc/.test('a-c');Result:
false
>
/^\w$/.test('aaa');Result:
false
We can match entire identifiers by combining
\wwith+and boundaries.>
/^\w+$/.test('my_variable');Result:
true
>
/^\w+$/.test('1+1');Result:
false
>
/^\w+$/.test('while');Result:
true
>
/^\w+$/.test('while (true)');Result:
false
As with any character class, upper-casing the class negates it. (
\Wis the opposite of\w.)>
/\w/.test('c');Result:
true
>
/\W/.test('c');Result:
false
>
/\w/.test('!');Result:
false
>
/\W/.test('!');Result:
true