Skip to content

Commit a1736bd

Browse files
authored
Merge pull request #2372 from vsemozhetbyt/9.10
Fix typos in 9.10 (Greedy and lazy quantifiers)
2 parents e1cb0f8 + 6567009 commit a1736bd

File tree

2 files changed

+6
-6
lines changed
  • 9-regular-expressions/10-regexp-greedy-and-lazy

2 files changed

+6
-6
lines changed

9-regular-expressions/10-regexp-greedy-and-lazy/4-find-html-tags-greedy-lazy/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ let str = '<> <a href="/"> <input type="radio" checked> <b>';
1212
alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
1313
```
1414

15-
Here we assume that tag attributes may not contain `<` and `>` (inside squotes too), that simplifies things a bit.
15+
Here we assume that tag attributes may not contain `<` and `>` (inside quotes too), that simplifies things a bit.

9-regular-expressions/10-regexp-greedy-and-lazy/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ These common words do not make it obvious why the regexp fails, so let's elabora
8888

8989
That's probably not what we expected, but that's how it works.
9090

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.**
9292

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

@@ -109,7 +109,7 @@ let regexp = /".+?"/g;
109109

110110
let str = 'a "witch" and her "broom" is one';
111111

112-
alert( str.match(regexp) ); // witch, broom
112+
alert( str.match(regexp) ); // "witch", "broom"
113113
```
114114

115115
To clearly understand the change, let's trace the search step by step.
@@ -179,7 +179,7 @@ let regexp = /"[^"]+"/g;
179179

180180
let str = 'a "witch" and her "broom" is one';
181181

182-
alert( str.match(regexp) ); // witch, broom
182+
alert( str.match(regexp) ); // "witch", "broom"
183183
```
184184

185185
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.
@@ -293,9 +293,9 @@ alert( str2.match(regexp) ); // <a href="link1" class="doc">, <a href="link2" cl
293293
Quantifiers have two modes of work:
294294

295295
Greedy
296-
: 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.
297297

298298
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.
300300

301301
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

Comments
 (0)