RegexpGarden




















- Garden Text Representation
12
let garden = "Rose\tRosemary\tRose\tRose\tPrimrose\t
Rosemary\tRose\tRose\tRose\tRosemary";
- Code Editor
- Log
123456
garden = garden
.replace(//gi, "Blackberry");
Lesson task •
The rose should be replaced with blackberry if it is followed directly by rosemary or by another rose that should be replaced
Positive lookahead
(?=...) again
/x(?=y)/ matches "x" only if it is directly followed by "y"Positive lookaheads can include nested capturing and non-capturing groups witch allows to form complex conditions. Let's say we need to replace with "bar" the first "foo" in the sequence of three "foo" followed up directly by "42":
1234
console.log("foo foo foo 42".replace(/foo(?=(\s*foo\s*){2}42)/gi, "bar")) // bar foo foo 42
console.log("foo foo 42".replace(/foo(?=(\s*foo\s*){2}42)/gi, "bar")) // foo foo 42
console.log("foo 42".replace(/foo(?=(\s*foo\s*){2}42)/gi, "bar")) // foo 42
console.log("foo foo foo foo 42".replace(/foo(?=(\s*foo\s*){2}42)/gi, "bar")) // foo bar foo foo 42