# 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 "match `pattern:x` only if followed by `pattern:y`". The euro sign is often written after the amount, so the regexp will be `pattern:\d+(?=€)` (assuming the price has no decimal point): ```js run let str = "1 turkey costs 30€"; alert( str.match(/\d+(?=€)/) ); // 30 (correctly skipped the sole number 1) ``` Or, if we wanted a quantity, then a negative lookahead can be applied. The syntax is: `pattern:x(?!y)`, it means "match `pattern:x` 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 Lookbehind 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:(?