Groups: (...)Les groupes : (...)
A quantifier applies to the element in front of it: a character, a class… or a group. Parentheses create that group, and give the quantifier something bigger to count:
/(ha)+/→ “ha”, “haha”, “hahaha”…;/(anti)?virus/→ “virus” or “antivirus”;/(\d{3}-){2}/→ “three digits and a hyphen”, twice over.
The second example is the most useful in practice. Making one character
optional with ? is common; making a whole portion of the pattern optional, a
prefix, a decimal part, an area code, takes a group.
ha haha haaa hahaa haaahaha hihi hohoha haha haaa hahaa haaahaha hihi hoho ha haha haaa hahaa haaahaha ha haha haaa hahaa haaahaha Je réécris ce texte, je le rererelis plusieurs fois. Il faut le réorganiser, le relire et y remédier, mais pas prendre, corriger, arrêter ni créer.Je réécris ce texte, je le rererelis plusieurs fois. Il faut le réorganiser, le relire et y remédier, mais pas prendre, corriger, arrêter ni créer. réécris rererelis réorganiser relire remédier réécris rererelis réorganiser relire remédier Call 514-123-4567 or 438-987-6543. My fax is 555-0000. Code: 123-45. Date: 2024-01-15.Appelez le 514-123-4567 ou le 438-987-6543. Mon fax est 555-0000. Code: 123-45. Date: 2024-01-15. 514-123-4567 438-987-6543 514-123-4567 438-987-6543 Un quantificateur porte sur l’élément qui le précède : un caractère, une classe… ou un groupe. Les parenthèses créent ce groupe, et donnent au quantificateur quelque chose de plus gros à compter :
/(ha)+/→ « ha », « haha », « hahaha »… ;/(anti)?virus/→ « virus » ou « antivirus » ;/(\d{3}-){2}/→ deux fois de suite « trois chiffres et un tiret ».
Le deuxième exemple est le plus utile en pratique. Rendre un caractère
optionnel avec ? est courant ; rendre optionnelle une portion entière du
motif, un préfixe, une partie décimale, un indicatif régional, demande un
groupe.
ha haha haaa hahaa haaahaha hihi hohoha haha haaa hahaa haaahaha hihi hoho ha haha haaa hahaa haaahaha ha haha haaa hahaa haaahaha Je réécris ce texte, je le rererelis plusieurs fois. Il faut le réorganiser, le relire et y remédier, mais pas prendre, corriger, arrêter ni créer.Je réécris ce texte, je le rererelis plusieurs fois. Il faut le réorganiser, le relire et y remédier, mais pas prendre, corriger, arrêter ni créer. réécris rererelis réorganiser relire remédier réécris rererelis réorganiser relire remédier Call 514-123-4567 or 438-987-6543. My fax is 555-0000. Code: 123-45. Date: 2024-01-15.Appelez le 514-123-4567 ou le 438-987-6543. Mon fax est 555-0000. Code: 123-45. Date: 2024-01-15. 514-123-4567 438-987-6543 514-123-4567 438-987-6543 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