A pot
Rose
A pot
Rosemary
A pot
Primrose
A pot
Rose
A pot
Rose
A pot
Primrose
A pot
Rosemary
A pot
Rose
A pot
Rose
A pot
Rosemary
A pot
Rose
1234
let garden = "Rose\tRosemary\tPrimrose\t
Rose\tRose\tPrimrose\t
Rosemary\tRose\tRose\t
Rosemary\tRose";
123456
garden = garden
.replace(//gi, "Blackberry");
Lesson 35 / 42

Lesson task •

The rose should be replaced with blackberry if it is followed directly by NOT rosemary or by another rose that should be replaced

Negative Lookahead

(?!...) again

/x(?!y)/ matches "x" only if it is not directly followed by "y"

Negative lookaheads, like Positive Lookaheads, can include nested capturing and non-capturing groups:

1234
console.log("foo foo foo foo 42".replace(/foo(?!(\s*foo\s*){2}42)/gi, "bar")) // bar foo bar bar 42 
console.log("foo foo foo 42".replace(/foo(?!(\s*foo\s*){2}42)/gi, "bar")) // foo bar bar 42 
console.log("foo foo 42".replace(/foo(?!(\s*foo\s*){2}42)/gi, "bar")) // bar bar 42 
console.log("foo 42".replace(/foo(?!(\s*foo\s*){2}42)/gi, "bar")) // bar 42