Skip to content

Commit 728998b

Browse files
committed
regexp
1 parent 7c6cfeb commit 728998b

File tree

1 file changed

+9
-5
lines changed
  • 9-regular-expressions/14-regexp-lookahead-lookbehind

1 file changed

+9
-5
lines changed

9-regular-expressions/14-regexp-lookahead-lookbehind/article.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ alert( str.match(/(?<!\$)\d+/) ); // 2 (skipped the price)
6060

6161
Generally, what's inside the lookaround (a common name for both lookahead and lookbehind) parentheses does not become a part of the match.
6262

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:€`.
6464

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.
6666

6767
For instance, here the currency `pattern:(€|kr)` is captured, along with the amount:
6868

@@ -89,9 +89,13 @@ Usually parentheses are numbered left-to-right, but lookbehind is an exception,
8989

9090
## Summary
9191

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.
9393

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.
9599

96100
Lookaround types:
97101

@@ -102,4 +106,4 @@ Lookaround types:
102106
| `pattern:(?<=y)x` | Positive lookbehind | `x` if after `y` |
103107
| `pattern:(?<!y)x` | Negative lookbehind | `x` if not after `y` |
104108

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

Comments
 (0)