Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 20, 2021 05:58 am GMT

Cheatsheet for the Regular Expressions Cheatsheet [Part 1]

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


Original Link: https://dev.to/mathlete/cheatsheet-for-the-regular-expressions-cheatsheet-part-1-247f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To