DEV Community

loading...
Cover image for Cheatsheet for the Regex Cheatsheet, Part IV: Assertions

Cheatsheet for the Regex Cheatsheet, Part IV: Assertions

Analogy | Absence | Example
The way I understand things is in relation to other things: Analogy: what can it be compared to? Absence: what would we have to do without it? Example: how does it work?
Updated on ・2 min read

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

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 lion roared
  • Example on regex101.com
  • Example in 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 ]
Enter fullscreen mode Exit fullscreen mode

?! Negative lookahead
  • ?! is used in /lion (?!yawned)/ to find the following: The lion roared
  • Example on regex101.com
  • Example in 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 ]
Enter fullscreen mode Exit fullscreen mode

?<= Lookbehind assertion
  • ?<= is used in /(?<=lion) roared/ to find the following: The lion roared
  • Example on regex101.com
  • Example in 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 ]
Enter fullscreen mode Exit fullscreen mode

?<! Negative lookbehind
  • ?<! is used in /(?<!asparagus) roared/ to find the following: The lion roared
  • Example on regex101.com
  • Example in 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 ]
Enter fullscreen mode Exit fullscreen mode

Regex Assertions that are not supported in Javascript

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

Discussion (0)