Skip to content

Word boundary: \b #274

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 5 commits into from
Jan 12, 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,5 +1,5 @@

The answer: `pattern:\b\d\d:\d\d\b`.
Réponse : `pattern:\b\d\d:\d\d\b`.

```js run
alert( "Breakfast at 09:00 in the room 123:456.".match( /\b\d\d:\d\d\b/ ) ); // 09:00
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Find the time
# Trouvez l'heure

The time has a format: `hours:minutes`. Both hours and minutes has two digits, like `09:00`.
L'heure à un format : `hours:minutes`. Les heures et les minutes ont deux chiffres, comme `09:00`.

Make a regexp to find time in the string: `subject:Breakfast at 09:00 in the room 123:456.`
Écrire une expression régulière pour trouver l'heure dans la chaîne de caractère : `subject:Breakfast at 09:00 in the room 123:456.`

P.S. In this task there's no need to check time correctness yet, so `25:99` can also be a valid result.
P.S. Dans cet exercice il n'y a pas besoin de vérifier la validité de l'heure, donc ici `25:99` peut être une correspondance valable.

P.P.S. The regexp shouldn't match `123:456`.
P.P.S. L'expression régulière ne doit pas valider `123:456`.
42 changes: 21 additions & 21 deletions 9-regular-expressions/06-regexp-boundary/article.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
# Word boundary: \b
# Limite de mot : \b

A word boundary `pattern:\b` is a test, just like `pattern:^` and `pattern:$`.
Une limite de mot `pattern:\b` teste une position, de la même manière que les ancres `pattern:^` et `pattern:$`.

When the regexp engine (program module that implements searching for regexps) comes across `pattern:\b`, it checks that the position in the string is a word boundary.
Quand le moteur d'expression régulière (module qui implémente la recherche d'expressions régulières) trouve le motif `pattern:\b`, il vérifie si la position dans la chaine de caractères est une limite de mot.

There are three different positions that qualify as word boundaries:
Il y a trois positions possibles pour une limite de mot :

- At string start, if the first string character is a word character `pattern:\w`.
- Between two characters in the string, where one is a word character `pattern:\w` and the other is not.
- At string end, if the last string character is a word character `pattern:\w`.
- Au début de la chaîne de caractères, si le premier caractère est alphanumérique (ou un trait de soulignement), c'est-à-dire qu'il correspond au motif `pattern:\w`.
- Entre deux caractères d'une chaîne, si seulement l'un des caractères correspond au motif `pattern:\w`, (alphanumérique ou trait de soulignement).
- À la fin de la chaîne de caractères, si le dernier caractère correspond au motif `pattern:\w`.

For instance, regexp `pattern:\bJava\b` will be found in `subject:Hello, Java!`, where `subject:Java` is a standalone word, but not in `subject:Hello, JavaScript!`.
Par exemple l'expression régulière `pattern:\bJava\b` sera trouvé dans `subject:Hello, Java!`, `subject:Java` est un mot isolé, mais pas dans `subject:Hello, JavaScript!`.

```js run
alert( "Hello, Java!".match(/\bJava\b/) ); // Java
alert( "Hello, JavaScript!".match(/\bJava\b/) ); // null
```

In the string `subject:Hello, Java!` following positions correspond to `pattern:\b`:
Dans la chaîne `subject:Hello, Java!` les positions suivantes correspondent au motif `pattern:\b`:

![](hello-java-boundaries.svg)

So, it matches the pattern `pattern:\bHello\b`, because:
Cette chaîne passe le test du motif `pattern:\bHello\b`, car :

1. At the beginning of the string matches the first test `pattern:\b`.
2. Then matches the word `pattern:Hello`.
3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a comma.
1. Le début de la chaîne passe le premier test `pattern:\b`.
2. Puis trouve le mot `pattern:Hello`.
3. Enfin le test `pattern:\b` est encore valide, comme nous sommes entre `subject:o` et une virgule.

So the pattern `pattern:\bHello\b` would match, but not `pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it).
Donc le motif `pattern:\bHello\b` sera trouvé, mais pas `pattern:\bHell\b` (car il n'y a pas de limite de mot après `l`) ni `Java!\b` (car le point d'exclamation ne correspond pas au motif `pattern:\w`, il n'est donc pas suivi par une limite de mot).

```js run
alert( "Hello, Java!".match(/\bHello\b/) ); // Hello
alert( "Hello, Java!".match(/\bJava\b/) ); // Java
alert( "Hello, Java!".match(/\bHell\b/) ); // null (no match)
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (no match)
alert( "Hello, Java!".match(/\bHell\b/) ); // null (aucune correspondance)
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (aucune correspondance)
```

We can use `pattern:\b` not only with words, but with digits as well.
La limite de mot `pattern:\b` ne s'utilise pas uniquement pour des mots, mais aussi pour les nombres.

For example, the pattern `pattern:\b\d\d\b` looks for standalone 2-digit numbers. In other words, it looks for 2-digit numbers that are surrounded by characters different from `pattern:\w`, such as spaces or punctuation (or text start/end).
Par exemple, le motif `pattern:\b\d\d\b` recherche un nombre isolé à deux chiffres. C'est-à-dire, qu'il cherche un nombre à deux chiffres entouré par des caractères qui ne correspondent pas au motif `pattern:\w`, comme des espaces, une ponctuation, un début ou une fin de chaîne.

```js run
alert( "1 23 456 78".match(/\b\d\d\b/g) ); // 23,78
alert( "12,34,56".match(/\b\d\d\b/g) ); // 12,34,56
```

```warn header="Word boundary `pattern:\b` doesn't work for non-latin alphabets"
The word boundary test `pattern:\b` checks that there should be `pattern:\w` on the one side from the position and "not `pattern:\w`" - on the other side.
```warn header="La limite de mot `pattern:\b` ne fonctionne pas pour des alphabets non latin"
Le test de limite de mot `pattern:\b` vérifie qu'il doit y avoir `pattern:\w` d'un côté de la position et "not `pattern:\w`" - de l'autre côté.

But `pattern:\w` means a latin letter `a-z` (or a digit or an underscore), so the test doesn't work for other characters, e.g. cyrillic letters or hieroglyphs.
Comme `pattern:\w` signifie `a-z`(en minuscule ou majuscule), un chiffre ou un trait de soulignement, le test ne fonctionne pas pour d'autres caractères, p. ex. lettre cyrillique ou idéogramme.
```