Skip to content

Greedy and lazy quantifiers #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

The result is: `match:123 4`.
La réponse est : `match:123 4`.

First the lazy `pattern:\d+?` tries to take as little digits as it can, but it has to reach the space, so it takes `match:123`.
Pour commencer, le motif paresseux `pattern:\d+?` essaye de prendre aussi peu de chiffres que possible, mais il doit atteindre ensuite un espace, donc il prend `match:123`.

Then the second `\d+?` takes only one digit, because that's enough.
Ensuite le second `\d+?` prend seulement un chiffre, parce que ça suffit.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A match for /d+? d+?/
# Correspondance pour /d+? d+?/

What's the match here?
Quel est la correspondance trouvée ici ?

```js
alert( "123 456".match(/\d+? \d+?/g) ); // ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
We need to find the beginning of the comment `match:<!--`, then everything till the end of `match:-->`.
Nous devons trouver le début d'un commentaire `match:<!--`, puis tout jusqu'à la fin de `match:-->`.

An acceptable variant is `pattern:<!--.*?-->` -- the lazy quantifier makes the dot stop right before `match:-->`. We also need to add flag `pattern:s` for the dot to include newlines.
Une expression régulière possible est `pattern:<!--.*?-->` -- le quantificateur paresseux arrête le point juste avant `match:-->`. Nous avons aussi besoin du marqueur `pattern:s` pour que le point inclue les nouvelles lignes.

Otherwise multiline comments won't be found:
Les commentaires multi-lignes ne seraient pas trouvés sans ce marqueur :

```js run
let regexp = /<!--.*?-->/gs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Find HTML comments
# Trouvez des commentaires HTML

Find all HTML comments in the text:
Trouvez tous les commentaires HTML dans le texte :

```js
let regexp = /your regexp/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The solution is `pattern:<[^<>]+>`.
Une solution `pattern:<[^<>]+>`.

```js run
let regexp = /<[^<>]+>/g;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Find HTML tags
# Trouver des balises HTML

Create a regular expression to find all (opening and closing) HTML tags with their attributes.
Écrire une expression régulière pour trouver toutes les balises HTML (ouvrantes et fermantes) avec leurs attributs.

An example of use:
Exemple d'usage :

```js run
let regexp = /your regexp/g;
Expand All @@ -12,4 +12,4 @@ let str = '<> <a href="/"> <input type="radio" checked> <b>';
alert( str.match(regexp) ); // '<a href="/">', '<input type="radio" checked>', '<b>'
```

Here we assume that tag attributes may not contain `<` and `>` (inside quotes too), that simplifies things a bit.
Pour simplifier un peu, nous considérons ici qu'une balise ne peut pas contenir de `<` ou `>` (même à l'intérieur de guillemets).
216 changes: 108 additions & 108 deletions 9-regular-expressions/10-regexp-greedy-and-lazy/article.md

Large diffs are not rendered by default.