Here is the video version for this topic. You can read the written version which comes after the video section.

From the previous lesson lookahead patterns , we saw how to match a string when it is "followed by" another string, and also when it is "NOT followed by" another string.

What about the characters behind a string? We can also try a check pattern for that. This is where lookbehind patterns come in.

A lookbehind pattern asserts that a match for a pattern, is preceded by another pattern. Let's see an example where we would use lookbehind patterns.

Say we have this string: β€œHe bought 50 items with $500”. We have two numbers here: 50 and 500. Let's say we want to match the number that represents the price he spent. We can use a lookbehind pattern here.

First let's look at the syntax of a lookbehind pattern.

/(?<=X)Y/

In the previous lesson, we saw that the syntax of a lookahead pattern is /Y(?=Z)/. Take note of the ?=. For lookbehind, the syntax is then ?<=.

Coming back to the string: "He bought 50 items with $500". We can match the number that represents the price with this pattern:

/(?<=\$)\d+/

The pattern starts with a lookbehind for "$". We escaped this character \$ because $ is a special character . Then we have one or more digits represented with \d+. So our pattern says "match one or more digits that is preceded with a dollar sign":

Regex/(?<=\$)\d+/g
Input
Match

As you can see here, 50 is not matched because it is preceded by an empty space " ". But 500 is matched because it is preceded by "$".