|
1 | | -# Sets and ranges [...] |
| 1 | +# Ensembles et intervalles [...] |
2 | 2 |
|
3 | | -Several characters or character classes inside square brackets `[…]` mean to "search for any character among given". |
| 3 | +Plusieurs caractères ou classes de caractères, entourés de crochets `[…]` signifie "chercher un caractère parmi ceux-là". |
4 | 4 |
|
5 | | -## Sets |
| 5 | +## Ensembles |
6 | 6 |
|
7 | | -For instance, `pattern:[eao]` means any of the 3 characters: `'a'`, `'e'`, or `'o'`. |
| 7 | +Par exemple, `pattern:[eao]` signifie un caractère qui est soit `'a'`, `'e'`, ou `'o'`. |
8 | 8 |
|
9 | | -That's called a *set*. Sets can be used in a regexp along with regular characters: |
| 9 | +Ceci est appelé un *ensemble*. Les ensembles peuvent être combinés avec d'autres caractères dans une expression rationnelle : |
10 | 10 |
|
11 | 11 | ```js run |
12 | | -// find [t or m], and then "op" |
| 12 | +// trouve [t ou m], puis "op" |
13 | 13 | alert( "Mop top".match(/[tm]op/gi) ); // "Mop", "top" |
14 | 14 | ``` |
15 | 15 |
|
16 | | -Please note that although there are multiple characters in the set, they correspond to exactly one character in the match. |
| 16 | +Bien qu'il y ait plusieurs caractères dans un ensemble, vous remarquez que l'on ne cherche la correspondance que d'un seul de ces caractères. |
17 | 17 |
|
18 | | -So the example below gives no matches: |
| 18 | +L'exemple suivant ne donne donc aucun résultat : |
19 | 19 |
|
20 | 20 | ```js run |
21 | | -// find "V", then [o or i], then "la" |
22 | | -alert( "Voila".match(/V[oi]la/) ); // null, no matches |
| 21 | +// trouve "V", puis [o ou i], puis "la" |
| 22 | +alert( "Voila".match(/V[oi]la/) ); // null, pas de correspondance |
23 | 23 | ``` |
24 | 24 |
|
25 | | -The pattern searches for: |
| 25 | +Le modèle recherche : |
26 | 26 |
|
27 | 27 | - `pattern:V`, |
28 | | -- then *one* of the letters `pattern:[oi]`, |
29 | | -- then `pattern:la`. |
| 28 | +- puis *une* des lettres `pattern:[oi]`, |
| 29 | +- enfin `pattern:la`. |
30 | 30 |
|
31 | | -So there would be a match for `match:Vola` or `match:Vila`. |
| 31 | +Ce qui correspondrait à `match:Vola` ou `match:Vila`. |
32 | 32 |
|
33 | | -## Ranges |
| 33 | +## Intervalles |
34 | 34 |
|
35 | | -Square brackets may also contain *character ranges*. |
| 35 | +Les crochets peuvent aussi contenir de *intervalles de caractères*. |
36 | 36 |
|
37 | | -For instance, `pattern:[a-z]` is a character in range from `a` to `z`, and `pattern:[0-5]` is a digit from `0` to `5`. |
| 37 | +Par exemple, `pattern:[a-z]` est un caractère pouvant aller de `a` à `z`, et `pattern:[0-5]` est un nombre pouvant valoir de `0` jusqu'à `5`. |
38 | 38 |
|
39 | | -In the example below we're searching for `"x"` followed by two digits or letters from `A` to `F`: |
| 39 | +Dans l'exemple ci-dessous nous recherchons un `"x"` suivi par deux chiffres ou lettres de `A` à `F`: |
40 | 40 |
|
41 | 41 | ```js run |
42 | 42 | alert( "Exception 0xAF".match(/x[0-9A-F][0-9A-F]/g) ); // xAF |
43 | 43 | ``` |
44 | 44 |
|
45 | | -Here `pattern:[0-9A-F]` has two ranges: it searches for a character that is either a digit from `0` to `9` or a letter from `A` to `F`. |
| 45 | +Ici `pattern:[0-9A-F]` comporte deux intervalles : il recherche un caractère qui est soit chiffre entre `0` et `9` compris ou bien une lettre entre `A` et `F` compris. |
46 | 46 |
|
47 | | -If we'd like to look for lowercase letters as well, we can add the range `a-f`: `pattern:[0-9A-Fa-f]`. Or add the flag `pattern:i`. |
| 47 | +Si nous voulons y inclure les lettres minuscules, nous pouvons ajouter l'intervalle `a-f`: `pattern:[0-9A-Fa-f]`. Ou bien ajouter le marqueur `pattern:i`. |
48 | 48 |
|
49 | | -We can also use character classes inside `[…]`. |
| 49 | +Nous pouvons aussi utiliser les classes de caractères entre `[…]`. |
50 | 50 |
|
51 | | -For instance, if we'd like to look for a wordly character `pattern:\w` or a hyphen `pattern:-`, then the set is `pattern:[\w-]`. |
| 51 | +Par exemple, si nous voulons chercher un caractère alphanumérique, un trait de soulignement `pattern:\w` ou un tiret `pattern:-`, alors l'ensemble s'écrit `pattern:[\w-]`. |
52 | 52 |
|
53 | | -Combining multiple classes is also possible, e.g. `pattern:[\s\d]` means "a space character or a digit". |
| 53 | +Il est aussi possible de combiner plusieurs classes, e.g. `pattern:[\s\d]` signifie "un caractère d'espacement ou un chiffre". |
54 | 54 |
|
55 | | -```smart header="Character classes are shorthands for certain character sets" |
56 | | -For instance: |
| 55 | +```smart header="Les classes de caractères sont en fait des racourcis pour des intervalles de caractères particuliers" |
| 56 | +Par exemple: |
57 | 57 |
|
58 | | -- **\d** -- is the same as `pattern:[0-9]`, |
59 | | -- **\w** -- is the same as `pattern:[a-zA-Z0-9_]`, |
60 | | -- **\s** -- is the same as `pattern:[\t\n\v\f\r ]`, plus few other rare Unicode space characters. |
| 58 | +- **\d** -- équivaut à `pattern:[0-9]`, |
| 59 | +- **\w** -- équivaut à `pattern:[a-zA-Z0-9_]`, |
| 60 | +- **\s** -- équivaut à `pattern:[\t\n\v\f\r ]`, plus quelques autres rares caractères unicodes d'espacement. |
61 | 61 | ``` |
62 | 62 |
|
63 | | -### Example: multi-language \w |
| 63 | +### Exemple : \w multi-langue |
64 | 64 |
|
65 | | -As the character class `pattern:\w` is a shorthand for `pattern:[a-zA-Z0-9_]`, it can't find Chinese hieroglyphs, Cyrillic letters, etc. |
| 65 | +Comme la classe de caractères `pattern:\w` est un raccourcis pour `pattern:[a-zA-Z0-9_]`, il ne peut pas trouver les idéogrammes chinois, ni les lettres cyrilliques, etc. |
66 | 66 |
|
67 | | -We can write a more universal pattern, that looks for wordly characters in any language. That's easy with Unicode properties: `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`. |
| 67 | +Nous pouvons écrire un modèle plus universel, qui cherche le caractère d'un mot quelle que soit la langue. On obtient facilement grâce aux propriétés Unicode : `pattern:[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]`. |
68 | 68 |
|
69 | | -Let's decipher it. Similar to `pattern:\w`, we're making a set of our own that includes characters with following Unicode properties: |
| 69 | +Déchiffrons cela. De la même manière que `pattern:\w`, nous construisons notre propre ensemble qui contient les caractères qui portent les propriétés Unicode : |
70 | 70 |
|
71 | | -- `Alphabetic` (`Alpha`) - for letters, |
72 | | -- `Mark` (`M`) - for accents, |
73 | | -- `Decimal_Number` (`Nd`) - for digits, |
74 | | -- `Connector_Punctuation` (`Pc`) - for the underscore `'_'` and similar characters, |
75 | | -- `Join_Control` (`Join_C`) - two special codes `200c` and `200d`, used in ligatures, e.g. in Arabic. |
| 71 | +- `Alphabetic` (`Alpha`) - pour les lettres, |
| 72 | +- `Mark` (`M`) - pour les accents, |
| 73 | +- `Decimal_Number` (`Nd`) - pour les nombres, |
| 74 | +- `Connector_Punctuation` (`Pc`) - pour le trait de soulignement `'_'` et autres caractères similaires, |
| 75 | +- `Join_Control` (`Join_C`) - deux codes spéciaux `200c` et `200d`, utilisés comme liaisons, e.g. en arabe. |
76 | 76 |
|
77 | | -An example of use: |
| 77 | +Exemple d'usage : |
78 | 78 |
|
79 | 79 | ```js run |
80 | 80 | let regexp = /[\p{Alpha}\p{M}\p{Nd}\p{Pc}\p{Join_C}]/gu; |
81 | 81 |
|
82 | 82 | let str = `Hi 你好 12`; |
83 | 83 |
|
84 | | -// finds all letters and digits: |
| 84 | +// trouve toutes les lettres et chiffres: |
85 | 85 | alert( str.match(regexp) ); // H,i,你,好,1,2 |
86 | 86 | ``` |
87 | 87 |
|
88 | | -Of course, we can edit this pattern: add Unicode properties or remove them. Unicode properties are covered in more details in the article <info:regexp-unicode>. |
| 88 | +Bien sûr, nous pouvons modifier cet ensemble : ajouter ou retirer des propriétés Unicode. Plus de détail sur ces propriétés Unicode dans l'article <info:regexp-unicode>. |
89 | 89 |
|
90 | | -```warn header="Unicode properties aren't supported in IE" |
91 | | -Unicode properties `pattern:p{…}` are not implemented in IE. If we really need them, we can use library [XRegExp](http://xregexp.com/). |
| 90 | +```warn header="Les propriétés Unicode ne sont pas supportées par IE" |
| 91 | +Les propriétés Unicode `pattern:p{…}` ne sont pas implémentées dans IE. Si nous en avons vraiment besoin, nous pouvons utiliser la librairie [XRegExp](http://xregexp.com/). |
92 | 92 |
|
93 | | -Or just use ranges of characters in a language that interests us, e.g. `pattern:[а-я]` for Cyrillic letters. |
| 93 | +Ou simplement utiliser des intervalles de caractères dans la langue qui nous intéresse, e.g. `pattern:[а-я]` pour les lettres cyrilliques. |
94 | 94 | ``` |
95 | 95 |
|
96 | 96 | ## Excluding ranges |
|
0 commit comments