Cheatsheet for the Regex Cheatsheet, Part VII: Groups & Ranges
Tags: beginners, javascript
Published: 2021-05-23 02:21:36

##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 VII: Groups & Ranges*

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

## What's are Groups & Ranges?
[It does exactly what it says on the tin](https://en.wikipedia.org/wiki/Does_exactly_what_it_says_on_the_tin#:~:text=%22It%20does%20exactly%20what%20it,accurate%20description%20of%20its%20qualities.&text=The%20phrase%20is%20also%20commonly%20known%20and%20used%20in%20Ireland.), as they say. Groups of characters and ranges of characters. 

## 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``

## Groups & Ranges

##### ``.`` Any character except new line (\n)
* ``.`` is used in ``/./g`` to find the following:  
*<mark>The lion roared</mark>``⮐``  
<mark>Again</mark>*
* Example on [regex101.com](https://regex101.com/r/jBIsna/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /./g;
let found = sentence.match(regex);
console.log(found); // [
  'T', 'h', 'e', ' ', 'l',
  'i', 'o', 'n', ' ', 'r',
  'o', 'a', 'r', 'e', 'd',
  ' ', ' ', 'A', 'g', 'a',
  'i', 'n'
]
```
---
##### ``(x|y)`` This character or that character (``x`` or ``y``)
* ``(a|b)`` is used in ``/(a|b)/`` to find the following:  
*The lion ro<mark>a</mark>red*
* Example on [regex101.com](https://regex101.com/r/y1QP5I/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /(a|b)/;
let found = sentence.match(regex);
console.log(found); // [ 'a' ]
```

---
##### ``[xyz]`` Range of characters (``x`` or ``y`` or ``z``)
* ``[aeiou]`` is used in ``/[aeiou]/g`` to find the following:  
*Th<mark>e</mark> l<mark>io</mark>n r<mark>oa</mark>r<mark>e</mark>d*
* Example on [regex101.com](https://regex101.com/r/Izi7jU/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /[aeiou]/g;
let found = sentence.match(regex);
console.log(found); // [ 'e', 'i', 'o', 'o', 'a', 'e' ]
```
---
##### ``[^xyz]`` Not a range of characters (``x`` or ``y`` or ``z``)
* ``[^aeiou]`` is used in ``/[^aeiou]/g`` to find the following:  
*<mark>Th</mark>e<mark> l</mark>io<mark>n r</mark>oa<mark>r</mark>e<mark>d</mark>*
* Example on [regex101.com](https://regex101.com/r/pYhzGR/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /[^aeiou]/g;
let found = sentence.match(regex);
console.log(found); // [
  'T', 'h', ' ',
  'l', 'n', ' ',
  'r', 'r', 'd'
]
```
---
##### ``[x-z]`` Span from this character to that character (``x`` through ``z``)
* ``[a-c]`` is used in ``/[a-c]/g`` to find the following:  
*The lion ro<mark>a</mark>red <mark>a</mark> <mark>b</mark>un<mark>c</mark>h of times*
* Example on [regex101.com](https://regex101.com/r/Ecso21/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared a bunch of times";
let regex = /[a-c]/g;
let found = sentence.match(regex);
console.log(found); // [ 'a', 'a', 'b', 'c' ]
```
---
##Dunce Corner

##### ``\x`` Group/­sub­pattern number "­x"
I don't get this. [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges) says:
*A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses). For example, ``/apple(,)\sorange\1/`` matches "apple, orange," in "apple, orange, cherry, peach".*
I have read this about five times and I still don't know what they're talking about. But I also have low blood sugar at the moment, so...