JavaScript Regular Expressions / lesson 7 of 11

Lookaheads and LookBehinds

Lookaheads and lookbehinds are zero-width assertions that check what's before or after a position without consuming characters.

Play
Transcript

[00:00] Okay, I’m going to drop in our little foo to foo boo guy. And we are going to take a look at lookaheads and lookbehinds. So lookaheads asserts what’s on the right and lookbehinds asserts what’s on the left. Regular expression is usually evaluated from the left to right. So right is ahead and left is behind. We know how to get foo. I’m going to save that. We can see we get that in our screen there. We know how to create a capture group to say I want foo followed by bar or boo, right? So we know how to do that. But let’s say what I want is foo. I don’t want bar or boo, but I only want foo when it’s followed by one of those guys.

[00:44] I can do a lookahead. So it’s question mark equals. This is a positive lookahead. So now I’ve got foo only where it is followed by bar or boo. That’s a positive. I can turn that into a negative lookahead with question mark exclamation point. And now I get foo where it’s not followed by bar or boo. Pretty easy. Pretty straightforward. So now let’s look at a look behind. So I’m going to create a capture group there. I’m going to get rid of my lookahead there. I’ll leave the group. That’s fine. And over here, I’m going to say question mark less than equals. So now I’m getting bar, but only where it is preceded. So where foo is to the left of it. So I’m asserting what’s on the left of it. And I can do a negative there as well and say not, but I don’t have one of those.

[01:44] So if I come up here and say F-U-B-A-R, now I’ve got bar where it is not preceded by F-O-O. So, okay, so we are going to try to do something a little more practical. We’re going to create a password validator. And we are going to start off with three capture groups. So it’s going to be uppercase. We’re going to say you have to have an uppercase letter. You have to have at least one lowercase letter. And you have to have at least one digit. And then we’ll say you have to have any of those characters from 8 to infinity. So you have to have at least a password of 8 characters. Now, each of these is going to be a look ahead. So I’m just going to drop that into each one.

[02:22] Let me see what we’re doing here. And then here, I’m going to say 0 or more of this character class. So that’ll be A to Z. And we’ll do the same thing here. 0 or more lowercase A to Z. And then 0 or more digits. I think that’ll work. So the way we’re going to test this is we’ll just console.log. regex.test our string. I’ll bring up the dev tools. And we can see it’s currently false because we do have enough characters. Let me strip that down really quick. Okay. So P, got our capital P. We got a number. But our problem right now is we are not long enough. So if I get to password 1, now it’s true. If I take off the number, now it’s false.

[03:10] So using almost nothing but lookaheads and simple character classes, we have created a groovy little password validator. So that is a quick look at lookaheads and lookbehinds.

Lookaheads and lookbehinds are zero-width assertions that check what’s before or after a position without consuming characters. This lesson covers positive lookahead (?=…), negative lookahead, positive lookbehind, and negative lookbehind. You’ll build a real-world password validator that enforces uppercase letters, lowercase letters, digits, and a minimum length — all using nothing but lookaheads.

app.js

app.ts
import output from "./output.js";
let str = `foo
foobar
foobaz
fooboo`;
let regex = /foo(bar)/g;
// lookahead asserts what's on the right
regex = /foo(?=bar|boo)/g; // is foo followed by bar?
regex = /foo(?!bar|boo)/g; // is foo NOT followed by bar?
// lookabehind asserts what's on the left
regex = /(?<=foo)(bar)/g;
regex = /(?<!foo)(bar)/g;
regex = /(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}/;
str = `Password1`;
console.log(regex.test(str));
output(str, regex);

Share this post on:

Previous
Capturing Groups
Next
Backreferences