JavaScript Regular Expressions / lesson 11 of 11

Set Operations

Set operations are a modern addition to JavaScript regex, enabled by the V flag.

Play
Transcript

[00:00] Set operations in JavaScript regular expressions are a newer and interesting syntax change, a way, a method to do some of the same things we’ve been doing in this series, but in a new way. So let’s check them out. In my string here, I’m representing a license plate. So it’s AIC03Z, and a rule is gonna be that we don’t wanna allow any of these ambiguous characters like zero, one, or capital I, or capital zero. So we’re gonna start off creating the character set. So I’m gonna say A to Z and zero to nine. So we’re accepting all of those right now.

[00:47] I think the most efficient way to do this would be using negative look ahead. So I’m gonna say negative look ahead, and I’m just gonna drop these guys into this character class. And now we get our A, Z, three, and Z. So there’s nothing wrong with that. This is the regular way to do it. It seems perfectly efficient.

[01:06] We are gonna jump over to enable set operations by adding the V flag. And now what we can do, so I’m gonna take our character set that we want, put it at the beginning. I’m gonna drop off our look ahead. And now I’m gonna wrap both of these in an outer character class. And now all I need to do is say, I want what’s on the left minus what’s on the right. So this set operation is subtraction. So give me what’s on the left, but not what’s on the right. And when I do that, we get the exact same results. So that’s pretty cool.

[01:42] And it’s, I guess I should say, it’s also a little bit easier to read. Like the intent is more clear in my mind. Again, this is a preference, but the other thing we could do without changing anything around is we could create an intersection instead of subtraction. So I can say, and, and, and I’m gonna get everything that’s in both of these. So you can see, I’m actually capturing the uppercase I and the zero.

[02:08] One of the thing to mention here is the V flag can’t be used with the U flag, the Unicode flag, but the V flag does enable the Unicode property operations. So instead of A to Z, if I wanted, I can say P and then I want uppercase letters. And then I could replace the numbers with Pn, and we get the exact same result for our subtraction as well. So what we’ve done here is we’ve unioned these two, and we’ve subtracted these guys from the result of those unions. So there you go. That’s a quick look at set operations in JavaScript regular expressions.

Set operations are a modern addition to JavaScript regex, enabled by the V flag. This lesson covers two powerful operations: subtraction (minus sign -) and intersection (ampersand &&). You’ll learn to subtract character sets — like excluding ambiguous characters (0, 1, I, O) from a license plate pattern — and intersect sets to find characters common to both. We also cover how the V flag enables Unicode property escapes (\p{...}) and works alongside them.

app.js

app.ts
import output from "./output.js";
let str = `AIC03Z`;
// do not allow 0,1,I,O
let regex = /(?![01IO])[A-Z0-9]/g;
regex = /[[A-Z0-9]--[01IO]]/gv;
regex = /[[A-Z0-9]&&[01IO]]/gv;
output(str, regex);

Share this post on:

Previous
Line Anchors