Lookahead and Lookbehind
Lookahead Assertions
Lookahead assertions allow you to match a pattern only if it is followed (or not followed) by another pattern. Lookahead assertions do not consume characters in the string; they only assert whether a match is possible.
- Positive Lookahead (
?=
)- Matches a pattern only if it is followed by another pattern.
const regexPositiveLookahead = /hello(?= world)/;
console.log(regexPositiveLookahead.test('hello world')); // true (matches 'hello' followed by 'world')
console.log(regexPositiveLookahead.test('hello there')); // false (no match because 'hello' is not followed by 'world')
- Negative Lookahead (
?!
)- Matches a pattern only if it is not followed by another pattern.
const regexNegativeLookahead = /hello(?! world)/;
console.log(regexNegativeLookahead.test('hello world')); // false (no match because 'hello' is followed by 'world')
console.log(regexNegativeLookahead.test('hello there')); // true (matches 'hello' not followed by 'world')
Lookbehind Assertions
Lookbehind assertions allow you to match a pattern only if it is preceded (or not preceded) by another pattern. Lookbehind assertions do not consume characters in the string; they only assert whether a match is possible.
- Positive Lookbehind (
?<=
)- Matches a pattern only if it is preceded by another pattern.
const regexPositiveLookbehind = /(?<=hello )world/;
console.log(regexPositiveLookbehind.test('hello world')); // true (matches 'world' preceded by 'hello ')
console.log(regexPositiveLookbehind.test('goodbye world')); // false (no match because 'world' is not preceded by 'hello ')
- Negative Lookbehind (
?<!
)- Matches a pattern only if it is not preceded by another pattern.
const regexNegativeLookbehind = /(?<!hello )world/;
console.log(regexNegativeLookbehind.test('hello world')); // false (no match because 'world' is preceded by 'hello ')
console.log(regexNegativeLookbehind.test('goodbye world')); // true (matches 'world' not preceded by 'hello ')
Example: Combining Lookahead and Lookbehind
You can combine lookahead and lookbehind assertions to create more complex patterns. For example, to match a word that is both preceded by “foo” and followed by “bar”:
const regexLookaround = /(?<=foo )\w+(?= bar)/;
console.log(regexLookaround.test('foo hello bar')); // true (matches 'hello')
console.log(regexLookaround.test('foo world baz')); // false (no match because 'world' is not followed by 'bar')
console.log(regexLookaround.test('hello foo bar')); // false (no match because 'foo' does not precede 'hello')
Exercises
- Write a regular expression that matches a number only if it is followed by the word “dollars”.
- Create a Regex pattern that matches a word only if it is not preceded by the word “not”.
- Write a regular expression that matches the word “apple” only if it is both preceded by “green” and followed by “pie”.
Lookahead and lookbehind assertions are powerful tools for matching patterns based on context. In the next lesson, we will explore modifiers, which allow you to control the behavior of your Regex patterns.