Skip to content

Word boundary: \b #293

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 3 commits into from
Apr 22, 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,8 +1,8 @@
# Trova l'orario
# Trovate l'orario

L'orario ha il formato: `ore:minuti`. Sia ore sia minuti sono formati da due cifre, come `09:00`.

Crea una regexp per trovare l'orario nella stringa: `subject:Colazione alle 09:00 nella stanza 123:456.`
Create una regexp per trovare l'orario nella stringa: `subject:Breakfast at 09:00 in the room 123:456.`

P.S. In questo esercizio non c'è ancora bisogno di controllare la correttezza dell'orario, quindi `25:99` è considerato un risultato valido.
P.S. In questo esercizio non c'è bisogno di controllare la correttezza dell'orario, quindi `25:99` è considerato un risultato valido.
P.P.S. La regexp non dovrebbe restituire `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
# Confine di parola: \b

A word boundary `pattern:\b` is a test, just like `pattern:^` and `pattern:$`.
Il confine di parola `pattern:\b` è un test, proprio come `pattern:^` e `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.
Quando l'interprete delle regexp (il modulo software che cerca all'interno delle espressioni regolari) incontra `pattern:\b`, verifica se la posizione nella stringa sia un confine di parola.

There are three different positions that qualify as word boundaries:
Ci sono tre differenti posizioni che qualificano il confine di parola:

- 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`.
- Ad inizio stringa, se il primo carattere di essa è un carattere di parola `pattern:\w`.
- Tra due caratteri di una stringa, laddove il primo sia un carattere di parola `pattern:\w` e l'altro no.
- A fine stringa, se l'ultimo carattere è un carattere di parola `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!`.
Ad esempio, la regexp `pattern:\bJava\b` troverà corrispondenza in `subject:Hello, Java!`, dove `subject:Java` è una parola a sé, ma non troverà alcuna corrispondenza in `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`:
Nella stringa `subject:Hello, Java!` trovano riscontro in `pattern:\b` le seguenti posizioni:

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

So, it matches the pattern `pattern:\bHello\b`, because:
Pertanto la corrispondenza con il pattern `pattern:\bHello\b` viene trovata perché:

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. All'inizio della stringa trova riscontro con il primo test `pattern:\b`.
2. Successivamente identifica la parola `pattern:Hello`.
3. Infine trova ancora corrispondenza con il test `pattern:\b`, dal momento che la posizione corrente è tra una `subject:o` e una virgola.

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).
Viene quindi individuato il pattern `pattern:\bHello\b`, ma non `pattern:\bHell\b` (perché non c'è un confine di parola dopo la `l`) e non `Java!\b` (perché il punto esclamativo non è un carattere di parola `pattern:\w`, quindi non c'è confine di parola dopo di esso).

```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 (nessuna corrispondenza)
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (nessuna corrispondenza)
```

We can use `pattern:\b` not only with words, but with digits as well.
Possiamo usare `pattern:\b` anche con i numeri non solo con le parole.

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).
Il pattern `pattern:\b\d\d\b`, ad esempio, cerca due caratteri numerici a sé stanti. In altre parole, cerca un numero di due cifre delimitato da caratteri differenti da `pattern:\w`, come spazi o punteggiatura (o l'inizio/la fine della stringa).

```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="Il confine di parola `pattern:\b` funziona solo con l'alfabeto latino"
Il test di confine parola `pattern:\b` controlla che ci sia `pattern:\w` da un lato della posizione e che non ci sia `pattern:\w` dall'altro lato.

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.
Ma `pattern:\w` significa una lettera dell'alfabeto latino `a-z` (o un numero o un underscore), pertanto il test non è efficace per altri caratteri, es. caratteri cirillici o simboli grafici.
```