Just Learn Code

Mastering JavaScript Regex Lookaheads: A Complete Guide

Introduction to JavaScript regex lookahead

When working with regular expressions in JavaScript, you may come across the term “lookahead.” Lookaheads can be incredibly useful when you want to match a specific pattern but only if it’s followed by another specific pattern. In this article, we’ll take a look at what lookaheads are, how to use them with regex, and explore some examples of how they work.

What is a lookahead in regular expressions? A lookahead is a part of a regular expression that allows you to match X only if it is followed by Y.

It’s like a filter or a checkpoint that says, “Hey, only match X if it’s followed by Y.”

Think of it like a guard at the door that only lets people in if they have a ticket. The ticket is Y, and the person is X.

If you don’t have the ticket, you don’t get in. If you do have the ticket, you can pass.

Syntax of a lookahead

The syntax for a lookahead in regex is (?=Y)X. The regex engine reads this as “match X, but only if it’s followed by Y.”

For example, let’s say we have the string “15 feet in length.” If we want to match the number 15 only if it’s followed by the literal string “feet,” we can use the lookahead syntax like this:

d+(?=sfeet)

In this regex, d+ matches one or more digits, which is our X.

The lookahead (?=sfeet) ensures that the match will only occur if there’s a space and the word “feet” after the digits.

Example of using a lookahead

Let’s take a closer look at the example we just mentioned. We have the string “15 feet in length,” and we want to match the number 15 only if it’s followed by “feet.” Using the lookahead syntax from above, we get:

d+(?=sfeet)

The d+ part matches the digits “15.” The lookahead (?=sfeet) checks to make sure that the digits are followed by a space and the word “feet.” The result is a successful match of “15.”

Regex multiple lookaheads

What if we wanted to match even more specific patterns? This is where multiple lookaheads can come in handy.

Syntax for multiple lookaheads

The syntax for using multiple lookaheads in regex is as follows:

(?=Y)(?=Z)X

In this case, we have two lookaheads, with Y and Z both being the conditions that need to be met for a match.

How multiple lookaheads work

When using multiple lookaheads, the regex engine tests all possible combinations of X, Y, and Z to find matches.

For example, let’s say we have the string “15 feet in length and 20 meters in height.” We want to match the number 15 only if it’s followed by “feet,” and we want to match the number 20 only if it’s followed by “meters.” Our regex might look like this:

d+(?=sfeet)(?=sins*meters)

Breaking this down, d+ matches one or more digits (in this case, either 15 or 20).

The first lookahead (?=sfeet) ensures that the match is only made if the digits are followed by “feet.” The second lookahead (?=sins*meters) ensures that the match is only made if the digits are followed by “meters” after the word “in.”

When the regex engine encounters this regex, it first searches for a match that satisfies both conditions (15 and “feet,” and 20 and “meters”). If it finds a match, it moves on to the next possible combination of X, Y, and Z until all possible matches have been found.

In our example above, the regex would match “15” and “20” as separate matches because they meet the individual conditions for each respective lookahead.

Conclusion

In this article, we’ve taken a look at what JavaScript regex lookaheads are, how they work, and how to use them with multiple conditions. Lookaheads can be incredibly powerful tools for matching specific patterns in your code.

Remember, lookaheads are just one part of the larger world of regular expressions, so keep exploring and experimenting to find the best regex patterns for your needs. Happy coding!

3) Regex negative lookaheads

In addition to positive lookaheads, JavaScript regex also supports negative lookaheads. A negative lookahead allows you to match X but not Y.

This can be incredibly useful when you want to exclude a certain pattern from your search.

Using negative lookahead to match X but not Y

Let’s take a look at an example to better understand how negative lookaheads work. Say we have the string “I have 20 apples and 10 oranges.” If we want to match the number of apples but not the number of oranges, we can use a negative lookahead.

Our regex might look like this:

d+(?!soranges)

Breaking it down, d+ matches one or more digits (in this case, 20 for our apples). The negative lookahead (?!soranges) ensures that the match is successful only if the digits are not followed by the word “oranges.”

In other words, this regex says “match one or more digits, unless those digits are followed by a space and the word ‘oranges’.”

Syntax and explanation of a negative lookahead

The syntax for a negative lookahead is like that of a positive lookahead, but with an additional exclamation point (!) at the start:

(?!Y)X

This tells the regex engine to match X only if it is not followed by Y.

For example, let’s say we want to match the word “car” only if it’s not followed by the word “driver.” Our regex might look like this:

car(?! driver)

This regex matches the word “car” but only if it’s not followed by a space and the word “driver.”

Just like with positive lookaheads, negative lookaheads can also be combined with other patterns.

For example, if we wanted to match the word “car” only if it’s not followed by “driver” and not preceded by “race,” our regex might look like this:

(?

This regex reads as “match ‘car’ only if it’s not preceded by the word ‘race’ and not followed by the word ‘driver’.”

Summary

In this article, we’ve explored JavaScript regex lookaheads, including positive lookaheads, multiple lookaheads, and negative lookaheads. Positive lookaheads allow you to match patterns only if they are followed by other specific patterns.

Multiple lookaheads can be used to match even more specific patterns. Negative lookaheads allow you to match patterns but exclude certain other patterns.

By understanding regex lookaheads, you can create more powerful and precise regex expressions in your JavaScript code. Remember to experiment and practice with different patterns to find the best fit for your specific use case.

JavaScript regex lookaheads can be incredibly useful tools when working with regular expressions. Positive lookaheads allow you to match specific patterns only if they’re followed by other specific patterns, while multiple lookaheads can be used to match even more specific patterns.

Negative lookaheads allow you to match patterns but exclude certain other patterns. By understanding these concepts, you can create more powerful and precise regex expressions in your code.

Take the time to experiment and practice with different patterns to find the best fit for your specific use case.

Popular Posts