Cheatsheet for the Regex Cheatsheet, Part IV: Assertions
Tags: beginners, javascript
Published: 2021-05-04 00:20: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 IV: Assertions*

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

## What's an Assertion?
We use assertions when we want to *assert* a specific sequence and set of conditions for a match. It's really better explained with examples, so read on!

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

## Assertions

##### ``?=`` Lookahead assertion
* ``?=`` is used in ``/lion (?=roared)/`` to find the following: 
*The <mark>lion </mark>roared*
* Example on [regex101.com](https://regex101.com/r/Knznml/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /lion (?=roared)/;
let found = sentence.match(regex);
console.log(found); // [ 'lion ', index: 4, input: 'The lion roared', groups: undefined ]
```
---
##### ``?!`` Negative lookahead
* ``?!`` is used in ``/lion (?!yawned)/`` to find the following: 
*The <mark>lion </mark>roared*
* Example on [regex101.com](https://regex101.com/r/JYDl3o/1/)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /lion (?!yawned)/;
let found = sentence.match(regex);
console.log(found); // [ 'lion ', index: 4, input: 'The lion roared', groups: undefined ]
```
---
##### ``?<=`` Lookbehind assertion
* ``?<=`` is used in ``/(?<=lion) roared/`` to find the following: 
*The lion <mark> roared</mark>*
* Example on [regex101.com](https://regex101.com/r/GNCRln/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /(?<=lion) roared/;
let found = sentence.match(regex);
console.log(found); // [ ' roared', index: 8, input: 'The lion roared', groups: undefined ]
```
---
##### ``?<!`` Negative lookbehind
* ``?<!`` is used in ``/(?<!asparagus) roared/`` to find the following: 
*The lion <mark> roared</mark>*
* Example on [regex101.com](https://regex101.com/r/12wvjz/1)
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /(?<!asparagus) roared/;
let found = sentence.match(regex);
console.log(found); // [ ' roared', index: 8, input: 'The lion roared', groups: undefined ]
```
---
## Regex Assertions that are not supported in Javascript

##### ``?>`` Once-only subexpression
##### ``?()`` Condition [if then]
##### ``?()|`` Condition [if then else]
##### ``?#`` Comment