Use Shorthand Regular Expression Character Classes

Joe Maddalone
InstructorJoe Maddalone
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

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

[00:00] 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 0to 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.

[00:30] 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.

[01:04] 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.

[01:27] 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.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today