Cheatsheet for the Regular Expressions Cheatsheet [Part 1]

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. Tha…


This content originally appeared on DEV Community and was authored by Analogy | Absence | Example

Alt Text

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. 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: Anchors Edition. 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

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.

^ Start of string, or start of line in multi-line pattern_
let foo = new RegExp(/^The/)
let sentence = "The lion roared"

console.log(sentence.match(foo))
// [ 'The', index: 0, input: 'The lion roared', groups: undefined ]
\A Start of string
// This is not supported in Javascript!
$ End of string, or end of line in multi-line pattern
let foo = new RegExp(/roared$/)
let sentence = "The lion roared"

console.log(sentence.match(foo))
// [ 'roared', index: 9, input: 'The lion roared', groups: undefined ]
\Z Start of string
// This is *also* not supported in Javascript
\b Word boundary
let foo = new RegExp(/\blion\b/)
let sentence = "The lion roared"

console.log(sentence.match(foo))
// [ 'lion', index: 4, input: 'The lion roared', groups: undefined ]
\B Not word boundary
// I still have no idea what this means
< Start of word

and

> End of word

// I'm starting to think that neither of these are supported in Javascript either...

Okay, well, when I started this project I didn't realize that some of the expressions wouldn't be supported, and others would just totally baffle me. Consider this a work in progress as I gather more answers...?


This content originally appeared on DEV Community and was authored by Analogy | Absence | Example


Print Share Comment Cite Upload Translate Updates
APA

Analogy | Absence | Example | Sciencx (2021-04-20T05:58:53+00:00) Cheatsheet for the Regular Expressions Cheatsheet [Part 1]. Retrieved from https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/

MLA
" » Cheatsheet for the Regular Expressions Cheatsheet [Part 1]." Analogy | Absence | Example | Sciencx - Tuesday April 20, 2021, https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/
HARVARD
Analogy | Absence | Example | Sciencx Tuesday April 20, 2021 » Cheatsheet for the Regular Expressions Cheatsheet [Part 1]., viewed ,<https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/>
VANCOUVER
Analogy | Absence | Example | Sciencx - » Cheatsheet for the Regular Expressions Cheatsheet [Part 1]. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/
CHICAGO
" » Cheatsheet for the Regular Expressions Cheatsheet [Part 1]." Analogy | Absence | Example | Sciencx - Accessed . https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/
IEEE
" » Cheatsheet for the Regular Expressions Cheatsheet [Part 1]." Analogy | Absence | Example | Sciencx [Online]. Available: https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/. [Accessed: ]
rf:citation
» Cheatsheet for the Regular Expressions Cheatsheet [Part 1] | Analogy | Absence | Example | Sciencx | https://www.scien.cx/2021/04/20/cheatsheet-for-the-regular-expressions-cheatsheet-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.