Cheatsheet for the Regex Cheatsheet, Part 1: Anchors
Tags: beginners, javascript
Published: 2021-04-20 05:58:53

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

If you, kind reader, are shaking your head in pity, this article is not for you. Go away. For the rest of us, here is a *Cheatsheet for the Regular Expressions Cheatsheet, Part 1: Anchors*. If people like this, I'll follow it up with editions for the rest of the categories.

##"Anchors Edition"? Huh?
Ok, so the cheat sheet has eleven categories. I could barely get through the first one, which is Anchors, so I'm restricting this blog post to Anchors. The sad thing is that I could only figure out the first five Anchors of the total eight that are listed. Maybe some kind reader will illuminate me on how those other three bastards work, since my googling didn't get me there.

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

## What are "Anchors", anyways?
Unlike other regular expression tokens, Anchors don't match actual characters. Anchors match a position before, after, or between characters. You'll see what I mean once you see an example.

To demonstrate the following regular expressions, I'm going to use the *match()* method, which retrieves the result of the matching a string against a regular expression.

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

## Anchors

##### ``^`` Start of string, or start of line in multi-line pattern
* ``^`` is used in ``/^The/`` to find the following]: 
*<mark>The</mark> lion roared*
* Example on [regex101.com](https://regex101.com/r/59j48a/1) 
* Example in Javascript:
```javascript
let sentence = "The lion roared";
let regex = /^The/g;
let found = sentence.match(regex);
console.log(found) // [ 'The' ]
```
---
##### ``\A`` Start of string
* ``\A`` is used in ``/\A/`` to find the beginning string starts (where the pipe is):
*<mark>|</mark>The lion roared*
* Example on [regex101.com](https://regex101.com/r/420avx/1)
* Example in Javascript: 
```javascript
// This doesn't work in Javascript :(
```
---
##### ``$`` End of string, or end of line in multi-line pattern
* ``$`` is used in ``/$/`` to find the end of a string (where the pipe is):
*The lion roared<mark>|</mark>*
* Example on [regex101.com](https://regex101.com/r/TWE6cq/1)
* Example in Javascript
```javascript
let sentence = "The lion roared";
let regex = /$/;
let found = sentence.match(regex);
console.log(found);
// [ '', index: 15, input: 'The lion roared', groups: undefined ]
```
---
##### ``\Z`` Start of string
* ``\Z`` is used in ``/\Z/`` to find the where the string ends (where the pipe is):  
*The lion roared<mark>|</mark>*
* Example on [regex101.com](https://regex101.com/r/3aIEhx/1)
* Example in Javascript: 
```javascript
// This doesn't work in Javascript :(
```
---
##### ``\b`` Word boundary
* ``\b`` is used in ``/\b/g`` to find the areas between characters and spaces, like where the pipes are:
*<mark>|</mark>The<mark>|</mark> <mark>|</mark>lion<mark>|</mark> <mark>|</mark>roared<mark>|</mark>*
* Example on [regex101.com](https://regex101.com/r/gDcss3/1)
* Example in Javascript: 
```javascript
let sentence = "The lion roared";
let regex = /\b/g;
let found = sentence.match(regex);
console.log(found); // [ '', '', '', '', '', '' ]
```
---
##### ``\B`` _Not_ word boundary
* ``\B`` is used in ``/\B/g`` to find the areas where ``\b`` does _not_ match:
*T<mark>|</mark>h<mark>|</mark>e l<mark>|</mark>i<mark>|</mark>o<mark>|</mark>n r<mark>|</mark>o<mark>|</mark>a<mark>|</mark>r<mark>|</mark>e<mark>|</mark>d*
* Example on [regex101.com](https://regex101.com/r/WQO7XC/1)
* Example in Javascript: 
```javascript
let sentence = "The lion roared";
let regex = /\B/g;
let found = sentence.match(regex);
console.log(found); // [ '', '', '', '', '',
  '', '', '', '', '' ]
```
---
## Dunce Corner

##### ``\<`` Start of word
##### ``\>`` End of word

These are a mystery to me. I posted about this on [Stack Overflow](https://stackoverflow.com/questions/67183670/regex-in-javascript-are-and-supported?noredirect=1#comment118753771_67183670) and all I got was (1) a -1 vote and (2) a comment linking to [_yet another_ Regular Expression Cheatsheet](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet) (where ``\<`` and ``\>`` are *not shown*). Super helpful...

---
_Surprisingly, this has been my most popular post, so I have revamped it to be more helpful and explanatory and I'm gonna continue with a series! Next up is Character Classes!_