# Lookahead and lookbehind Sometimes we need to match a pattern only if followed by another pattern. For instance, we'd like to get the price from a string like `subject:1 turkey costs 30€`. We need a number (let's say a price has no decimal point) followed by `subject:€` sign. That's what lookahead is for. ## Lookahead The syntax is: `pattern:x(?=y)`, it means "look for `pattern:x`, but match only if followed by `pattern:y`". For an integer amount followed by `subject:€`, the regexp will be `pattern:\d+(?=€)`: ```js run let str = "1 turkey costs 30€"; alert( str.match(/\d+(?=€)/) ); // 30 (correctly skipped the sole number 1) ``` Let's say we want a quantity instead, that is a number, NOT followed by `subject:€`. Here a negative lookahead can be applied. The syntax is: `pattern:x(?!y)`, it means "search `pattern:x`, but only if not followed by `pattern:y`". ```js run let str = "2 turkeys cost 60€"; alert( str.match(/\d+(?!€)/) ); // 2 (correctly skipped the price) ``` ## Lookbehind Lookahead allows to add a condition for "what goes after". Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before. The syntax is: - Positive lookbehind: `pattern:(?<=y)x`, matches `pattern:x`, but only if it follows after `pattern:y`. - Negative lookbehind: `pattern:(?