This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Use shorthand regular expression character classes

1:42 JavaScript lesson by

In this lesson we'll learn shorthands for common character classes as well as their negated forms.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In this lesson we'll learn shorthands for common character classes as well as their negated forms.

Avatar
Evandro Myller

Fantastic! As a minor contribution, the \w shorthand will also match the underscore symbol, therefore being equivalent to [a-zA-Z0-9_].

In reply to egghead.io

If we wanted to match all alphanumeric characters we can use this character class of all lower case letters, all upper case letters, and all digits from 0 to 9. Save that, and we can see that we are in fact matching all of those characters. However there's a shorthand for this, and in this case it is \w, which is going to give us the exact same results, matching all of those characters, but not spaces, and not symbols or Unicode characters.

Now if we only want digits we can do \d, and now we're matching only the digits, and if we only want to match white space, we can do \s and now we're only matching the white space. Now since these are character classes they can be negated, so I'm creating a new character class, I'm starting it off with the caret symbol which is going to negate whatever follows it, and I can just drop my character class of alphanumeric right here after that, so now we are matching only white space and symbols, we're not matching any letters or numbers.

We can do the same thing with digits, so now we're matching everything that's not a digit. We can do the same thing with white space, and we're matching everything that's not white space. Now there's actually shorthands for the negated versions of each of these shorthands, and the first one would be \W, so that's going to negate the alphanumeric matching, so now we're matching white space and symbols.

If we want to negate matching digits we can do \D and now we're matching everything except for digits, and if we want to do the negation of white space, we can do \S and now we're matching everything except for white space.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?