Cheatsheet for the Regex Cheatsheet, Part II: Character Classes
Tags: beginners, javascript
Published: 2021-04-24 01:07:08

##Intro
I was recently doing a code challenge for a job interview that required me to strip out all nonalphabetic characters. "Ah! I should use Regular Expressions for this!" I thought in triumph, impressed that I even knew what regular expressions _were_. That fleeting moment of glory faded once I decided to brush up on regular expressions and landed on the encouragingly-named [Regular Expressions Cheatsheet](https://cheatography.com/davechild/cheat-sheets/regular-expressions/). I had no idea how to use it!

So, for people like me, here is a *Cheatsheet for the Regular Expressions Cheatsheet, Part 2: Character Classes*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0jpldizq41c8juqy0ib.png)

## What are Character Classes?
A character class is a special notation that matches any symbol from a certain set.

## Anatomy of a regular expression
1. Forward slashes go on either end like so: ``/``something``/``
2. Add ``g`` for "global" at the end to find every instance, like so: ``/``something``/g``
3. Add ``m`` to "multi line" to the beginning/end of each line, not just the beginning/end of each string, like ``/``something``/g`` or ``/``something``/gm``

## Character Classes

##### ``\s`` White space
* ``\s`` is used in ``/\s/gm`` to find the following]: 
*The<mark> </mark>lion<mark> </mark>roared*
* Example on [regex101.com](https://regex101.com/r/Zi4QSw/2)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /\s/gm;
let found = sentence.match(regex);
console.log(found); // [ ' ', ' ' ]
```
---
##### ``\S`` Not white space
* ``\S`` is used in ``/\S/gm`` to find the following]: 
*<mark>The</mark> <mark>lion</mark> <mark>roared</mark>*
* Example on [regex101.com](https://regex101.com/r/0xQnXh/1) 
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /\S/gm;
let found = sentence.match(regex);
console.log(found); //
[ 'T', 'h', 'e', 'l',
  'i', 'o', 'n', 'r',
  'o', 'a', 'r', 'e',
  'd']
```
---
##### ``\d`` Digit
* ``\d`` is used in ``/\d/gm`` to find the following]: 
*The <mark>3</mark> lions roared*
* Example on [regex101.com](https://regex101.com/r/Pfr2j9/1) 
* Example in Javascript:
```javascript
let sentence = "The 3 lions roared";
let regex = /\d/gm;
let found = sentence.match(regex);
console.log(found); // [ '3' ]
```
---
##### ``\D`` Not digit
* ``\D`` is used in ``/\D/gm`` to find the following]: 
*<mark>The </mark>3<mark> lions roared</mark>*
* Example on [regex101.com](https://regex101.com/r/bVzTHY/1) 
* Example in Javascript:
```javascript
let sentence = "The 3 lions roared";
let regex = /\D/gm;
let found = sentence.match(regex);
console.log(found); // 
[
  'T', 'h', 'e', ' ', ' ',
  'l', 'i', 'o', 'n', 's',
  ' ', 'r', 'o', 'a', 'r',
  'e', 'd'
]
```
---
##### ``\w`` Word
* ``\w`` is used in ``/\w/gm`` to find the following]: 
*<mark>The</mark> <mark>lion</mark> <mark>roared</mark>*
* Example on [regex101.com](lion) 
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /\w/gm;
let found = sentence.match(regex);
console.log(found); // [
  'T', 'h', 'e', 'l',
  'i', 'o', 'n', 'r',
  'o', 'a', 'r', 'e',
  'd'
]
```
---
##### ``\W`` Not word
* ``\W`` is used in ``/\W/gm`` to find the following]: 
*The<mark> </mark>lion<mark> </mark>roared*
* Example on [regex101.com](https://regex101.com/r/Cnr4wA/2) 
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /\W/gm;
let found = sentence.match(regex);
console.log(found); // [ ' ', ' ' ]
```
---
## Dunce Corner
I can't make any of the following work. Try them out and report back in the comments:

##### ``\c`` Control character: [Try on regex101.com](https://regex101.com/r/FUGeNs/1)

##### ``\x`` Hexadecimal digit: [Try on regex101.com](https://regex101.com/r/c4NBMC/1)

##### ``\O`` Octal digit: [Try on regex101.com](https://regex101.com/r/8B0kfs/1)