Greedy or lazyGourmand ou non gourmand
Applied to “<b>important</b> and <b>useful</b>”, the pattern
/<b>.*<\/b>/ finds one giant match, running from the first opening tag to
the last closing one. Quantifiers quantifierquantificateur regular expressionsexpressions rationnelles A regular-expression operator stating how many times the preceding element may repeat ({n}, {n,m}, *, +, ?); greedy by default, matching as much as possible, and made lazy with a following ?Opérateur d'expression rationnelle indiquant combien de fois l'élément précédent peut se répéter ({n}, {n,m}, *, +, ?); gourmand par défaut (il capture le plus possible), il devient non gourmand avec un ? supplémentaire. are greedy:
they take as many characters as they can, backing off only far enough for the
end of the pattern to still match.
To take as few as possible, you add ? after the quantifier:
*→ 0 or more, as many as possible;*?→ 0 or more, as few as possible;+→ 1 or more, as many as possible;+?→ 1 or more, as few as possible.
She said « hello » then « goodbye » to everyone. The straight quotes "hi" do not count.Elle a dit « bonjour » puis « au revoir » à tout le monde. Les guillemets anglais "hello" ne comptent pas. «␣bonjour␣» «␣au␣revoir␣» ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide «␣hello␣» «␣goodbye␣» ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide He says (hello) then (goodbye) before leaving. Note: [bracket] and {brace} do not count.Il dit (bonjour) puis (au revoir) avant de partir. Note: [crochet] et {accolade} ne comptent pas. (bonjour) (au␣revoir) ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide (hello) (goodbye) Appliqué à « <b>important</b> et <b>utile</b> », le motif /<b>.*<\/b>/
trouve une seule correspondance géante, qui va de la première balise ouvrante
à la dernière balise fermante. Les quantificateurs quantifierquantificateur regular expressionsexpressions rationnelles A regular-expression operator stating how many times the preceding element may repeat ({n}, {n,m}, *, +, ?); greedy by default, matching as much as possible, and made lazy with a following ?Opérateur d'expression rationnelle indiquant combien de fois l'élément précédent peut se répéter ({n}, {n,m}, *, +, ?); gourmand par défaut (il capture le plus possible), il devient non gourmand avec un ? supplémentaire. sont gourmands (greedy) : ils
prennent le plus de caractères possible, quitte à revenir en arrière juste
assez pour que la fin du motif corresponde encore.
Pour capturer le moins possible, on ajoute ? après le quantificateur :
*→ 0 ou plus, le plus possible ;*?→ 0 ou plus, le moins possible ;+→ 1 ou plus, le plus possible ;+?→ 1 ou plus, le moins possible.
She said « hello » then « goodbye » to everyone. The straight quotes "hi" do not count.Elle a dit « bonjour » puis « au revoir » à tout le monde. Les guillemets anglais "hello" ne comptent pas. «␣bonjour␣» «␣au␣revoir␣» ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide «␣hello␣» «␣goodbye␣» ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide He says (hello) then (goodbye) before leaving. Note: [bracket] and {brace} do not count.Il dit (bonjour) puis (au revoir) avant de partir. Note: [crochet] et {accolade} ne comptent pas. (bonjour) (au␣revoir) ␣ space · ⇥ tab · ⏎ line break · ◆ empty match␣ espace · ⇥ tabulation · ⏎ retour à la ligne · ◆ correspondance vide (hello) (goodbye) Sources
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
Sources
- MDN Web Docs (n.d.). Regular expressions. MDN JavaScript Guide, Mozilla. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions