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
Copy file name to clipboardExpand all lines: 9-regular-expressions/10-regexp-greedy-and-lazy/article.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ These common words do not make it obvious why the regexp fails, so let's elabora
88
88
89
89
That's probably not what we expected, but that's how it works.
90
90
91
-
**In the greedy mode (by default) a quantifier is repeated as many times as possible.**
91
+
**In the greedy mode (by default) a quantified character is repeated as many times as possible.**
92
92
93
93
The regexp engine adds to the match as many characters as it can for `pattern:.+`, and then shortens that one by one, if the rest of the pattern doesn't match.
94
94
@@ -109,7 +109,7 @@ let regexp = /".+?"/g;
109
109
110
110
let str ='a "witch" and her "broom" is one';
111
111
112
-
alert( str.match(regexp) ); // witch, broom
112
+
alert( str.match(regexp) ); //"witch", "broom"
113
113
```
114
114
115
115
To clearly understand the change, let's trace the search step by step.
@@ -179,7 +179,7 @@ let regexp = /"[^"]+"/g;
179
179
180
180
let str ='a "witch" and her "broom" is one';
181
181
182
-
alert( str.match(regexp) ); // witch, broom
182
+
alert( str.match(regexp) ); //"witch", "broom"
183
183
```
184
184
185
185
The regexp `pattern:"[^"]+"` gives correct results, because it looks for a quote `pattern:'"'` followed by one or more non-quotes `pattern:[^"]`, and then the closing quote.
: By default the regular expression engine tries to repeat the quantifier as many times as possible. For instance, `pattern:\d+` consumes all possible digits. When it becomes impossible to consume more (no more digits or string end), then it continues to match the rest of the pattern. If there's no match then it decreases the number of repetitions (backtracks) and tries again.
296
+
: By default the regular expression engine tries to repeat the quantified character as many times as possible. For instance, `pattern:\d+` consumes all possible digits. When it becomes impossible to consume more (no more digits or string end), then it continues to match the rest of the pattern. If there's no match then it decreases the number of repetitions (backtracks) and tries again.
297
297
298
298
Lazy
299
-
: Enabled by the question mark `pattern:?` after the quantifier. The regexp engine tries to match the rest of the pattern before each repetition of the quantifier.
299
+
: Enabled by the question mark `pattern:?` after the quantifier. The regexp engine tries to match the rest of the pattern before each repetition of the quantified character.
300
300
301
301
As we've seen, the lazy mode is not a "panacea" from the greedy search. An alternative is a "fine-tuned" greedy search, with exclusions, as in the pattern `pattern:"[^"]+"`.
0 commit comments