Character classesClasses de caractères
A character class is how you say “I want ONE character out of these”:
/[aeiou]/→ one lowercase vowel;/c[aou]t/→ “cat”, “cot”, “cut”, but not “cet”.
A class occupies the place of a single character, like the dot, but with an explicit list instead of “any one at all”. It is the first way of tightening a pattern that matches too much.
/fixe./ would also catch "fixer"./peu./ capturerait aussi « peur » et le début de « peuple ».The tool fixed the file and fixes the next. Do not fixate on it. A fixer is a person.Je peux traduire, mais il ne peut pas. Peu importe, on n'a pas peur. Le peuple veut la paix. peux peut fixed fixes Our solution to exercise 1 "fixed or fixes"exercice 1 « peux ou peut », /fixe./, found “fixed” and
“fixes” as well. The one here is better because it is more specific:
/fixe[ds]/ will not hand you an unexpected match on “fixer”.
The bat hit the bit in the but. Better bet on the bot.The bat hit the bit in the but. Better bet on the bot. bat bit but bat bit but Les classes de caractères permettent de dire « je veux UN caractère parmi ceux-ci » :
/[aeiou]/→ une voyelle minuscule ;/ch[aiou]t/→ « chat », « chit », « chot », « chut », mais pas « chet ».
Une classe occupe donc la place d’un seul caractère, comme le point, mais avec une liste explicite au lieu de « n’importe lequel ». C’est le premier moyen de resserrer un motif trop large.
/fixe./ would also catch "fixer"./peu./ capturerait aussi « peur » et le début de « peuple ».The tool fixed the file and fixes the next. Do not fixate on it. A fixer is a person.Je peux traduire, mais il ne peut pas. Peu importe, on n'a pas peur. Le peuple veut la paix. peux peut fixed fixes Notre solution pour l’exercise 1 "fixed or fixes"exercice 1 « peux ou peut », /peu./, trouvait elle
aussi « peux » et « peut ». Celle-ci est meilleure parce qu’elle est plus
précise : /peu[xt]/ ne vous donnera pas de correspondance inattendue sur
« peur ».
The bat hit the bit in the but. Better bet on the bot.The bat hit the bit in the but. Better bet on the bot. bat bit but bat bit but 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