You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generally, what's inside the lookaround (a common name for both lookahead and lookbehind) parentheses does not become a part of the match.
62
62
63
-
E.g. in the pattern `pattern:\d+(?!€)`, the `pattern:€` sign doesn't get captured as a part of the match.
63
+
E.g. in the pattern `pattern:\d+(?=€)`, the `pattern:€` sign doesn't get captured as a part of the match. That's natural: we look for a number `pattern:\d+`, while `pattern:(?=€)` is just a test that it should be followed by `subject:€`.
64
64
65
-
But if we want to capture the whole lookaround expression or a part of it, that's possible. Just need to wrap that into additional parentheses.
65
+
But in some situations we might want to capture the lookaround expression as well, or a part of it. That's possible. Just wrap that into additional parentheses.
66
66
67
67
For instance, here the currency `pattern:(€|kr)` is captured, along with the amount:
68
68
@@ -89,9 +89,13 @@ Usually parentheses are numbered left-to-right, but lookbehind is an exception,
89
89
90
90
## Summary
91
91
92
-
Lookahead and lookbehind (commonly referred to as "lookaround") are useful for simple regular expressions, when we'd like not to take something into the match depending on the context before/after it.
92
+
Lookahead and lookbehind (commonly referred to as "lookaround") are useful when we'd like to take something into the match depending on the context before/after it.
93
93
94
-
Sometimes we can do the same manually, that is: match all and filter by context in the loop. Remember, `str.matchAll` and `reg.exec` return matches with `.index` property, so we know where exactly in the text it is. But generally regular expressions can do it better.
94
+
For simple regexps we can do the similar thing manually. That is: match everything, in any context, and then filter by context in the loop.
95
+
96
+
Remember, `str.matchAll` and `reg.exec` return matches with `.index` property, so we know where exactly in the text it is, and can check the context.
97
+
98
+
But generally regular expressions are more convenient.
95
99
96
100
Lookaround types:
97
101
@@ -102,4 +106,4 @@ Lookaround types:
102
106
|`pattern:(?<=y)x`| Positive lookbehind |`x` if after `y`|
103
107
|`pattern:(?<!y)x`| Negative lookbehind |`x` if not after `y`|
104
108
105
-
Lookahead can also used to disable backtracking. Why that may be needed -- see in the next chapter.
109
+
Lookahead can also used to disable backtracking. Why that may be needed and other details -- see in the next chapter.
0 commit comments