Skip to content

Commit cf98758

Browse files
authored
Merge pull request #288 from longo-andrea/article/escaping-chars
Escaping, special characters
2 parents 5f3362b + cc51b29 commit cf98758

File tree

1 file changed

+33
-33
lines changed
  • 9-regular-expressions/07-regexp-escaping

1 file changed

+33
-33
lines changed
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,99 @@
11

2-
# Escaping, special characters
2+
# Escaping, caratteri speciali
33

4-
As we've seen, a backslash `pattern:\` is used to denote character classes, e.g. `pattern:\d`. So it's a special character in regexps (just like in regular strings).
4+
Come abbiamo visto, un backslash `pattern:\` viene utilizzato per indicare classi di caratteri, e.g. `pattern:\d`. Quindi nelle regexp è un carattere speciale (proprio come nelle stringhe).
55

6-
There are other special characters as well, that have special meaning in a regexp. They are used to do more powerful searches. Here's a full list of them: `pattern:[ \ ^ $ . | ? * + ( )`.
6+
Esistono anche altri caratteri speciali, che hanno un significato speciale nelle regexp. Vengono utilizzati per ricerche più avanzate. Qui vediamo la lista completa: `pattern:[ \ ^ $ . | ? * + ( )`.
77

8-
Don't try to remember the list -- soon we'll deal with each of them separately and you'll know them by heart automatically.
8+
Non provate ad imparare a memoria la lista, presto inizieremo ad utilizzarli e sarete in grado di ricordarli automaticamente.
99

1010
## Escaping
1111

12-
Let's say we want to find literally a dot. Not "any character", but just a dot.
12+
Ipotizziamo di voler trovare un punto (il carattere "."). Non "qualsiasi carattere", semplicemente un punto.
1313

14-
To use a special character as a regular one, prepend it with a backslash: `pattern:\.`.
14+
Per poter utilizzare un carattere speciale come se fosse uno normale, è sufficiente farlo precedere da un backslash: `pattern:\.`.
1515

16-
That's also called "escaping a character".
16+
Abbiamo appena fatto l'"escaping di un carattere".
1717

18-
For example:
18+
Ad esempio:
1919
```js run
20-
alert( "Chapter 5.1".match(/\d\.\d/) ); // 5.1 (match!)
21-
alert( "Chapter 511".match(/\d\.\d/) ); // null (looking for a real dot \.)
20+
alert( "Chapter 5.1".match(/\d\.\d/) ); // 5.1 (trovato!)
21+
alert( "Chapter 511".match(/\d\.\d/) ); // null (cercando un punto \.)
2222
```
2323

24-
Parentheses are also special characters, so if we want them, we should use `pattern:\(`. The example below looks for a string `"g()"`:
24+
Anche le parentesi sono dei caratteri speciali, quindi se volessimo utilizzarle, dovremmo utilizzare `pattern:\(`. L'esempio sotto cerca la stringa `"g()"`:
2525

2626
```js run
2727
alert( "function g()".match(/g\(\)/) ); // "g()"
2828
```
2929

30-
If we're looking for a backslash `\`, it's a special character in both regular strings and regexps, so we should double it.
30+
Se stiamo cercando un backslash `\`, che è un carattere speciale sia nelle stringhe che nelle regexp, dovremmo inserirne due.
3131

3232
```js run
3333
alert( "1\\2".match(/\\/) ); // '\'
3434
```
3535

36-
## A slash
36+
## Lo slash
3737

38-
A slash symbol `'/'` is not a special character, but in JavaScript it is used to open and close the regexp: `pattern:/...pattern.../`, so we should escape it too.
38+
Il simbolo di slash `'/'` non è un carattere speciale, ma in JavaScript viene utilizzato per aprire e chiudere le regexp: `pattern:/...pattern.../`, quindi dovremo fare l'escape anche di questo carattere.
3939

40-
Here's what a search for a slash `'/'` looks like:
40+
Così è come appare una regexp `'/'` che cerca uno slash:
4141

4242
```js run
4343
alert( "/".match(/\//) ); // '/'
4444
```
4545

46-
On the other hand, if we're not using `pattern:/.../`, but create a regexp using `new RegExp`, then we don't need to escape it:
46+
In alternativa, se non utilizziamo `pattern:/.../`, ma creiamo una regexp utilizzando `new RegExp`, allora non avremmo bisogno dell'escape:
4747

4848
```js run
49-
alert( "/".match(new RegExp("/")) ); // finds /
49+
alert( "/".match(new RegExp("/")) ); // trovato /
5050
```
5151

5252
## new RegExp
5353

54-
If we are creating a regular expression with `new RegExp`, then we don't have to escape `/`, but need to do some other escaping.
54+
Se stiamo creando un'espressione regolare con `new RegExp`, allora non sarà necessario l'escape di `/`, ma dovremmo fare altri escape.
5555

56-
For instance, consider this:
56+
Ad esempio, consideriamo il seguente esempio:
5757

5858
```js run
5959
let regexp = new RegExp("\d\.\d");
6060

6161
alert( "Chapter 5.1".match(regexp) ); // null
6262
```
6363

64-
The similar search in one of previous examples worked with `pattern:/\d\.\d/`, but `new RegExp("\d\.\d")` doesn't work, why?
64+
Un ricerca simile, in uno degli esempi precedenti, funzionava con `pattern:/\d\.\d/`, ma `new RegExp("\d\.\d")` non funziona, perché?
6565

66-
The reason is that backslashes are "consumed" by a string. As we may recall, regular strings have their own special characters, such as `\n`, and a backslash is used for escaping.
66+
Il motivo è che i backslash vengono "consumati" dalla stringa. Ricordate, le stringhe "normali" hanno i loro caratteri speciali, come `\n`, e un backslash viene utilizzato per fare escaping.
6767

68-
Here's how "\d\.\d" is preceived:
68+
Così è come "\d\.\d" viene percepita:
6969

7070
```js run
7171
alert("\d\.\d"); // d.d
7272
```
7373

74-
String quotes "consume" backslashes and interpret them on their own, for instance:
74+
Gli apici della stringa "consumano" i backslash e li interpreta come a se stanti, ad esempio:
7575

76-
- `\n` -- becomes a newline character,
77-
- `\u1234` -- becomes the Unicode character with such code,
78-
- ...And when there's no special meaning: like `pattern:\d` or `\z`, then the backslash is simply removed.
76+
- `\n`, diventa un carattere nuova riga,
77+
- `\u1234`, diventa il carattere Unicode con quel codice,
78+
- ...Ed in qualsiasi caso in cui c'è un significato speciale: come `pattern:\d` o `\z`, allora i backslash verranno semplicemente rimossi.
7979

80-
So `new RegExp` gets a string without backslashes. That's why the search doesn't work!
80+
Quindi `new RegExp` ottiene la stringa senza i backslash. Questo è il motivo per cui la ricerca non funziona!
8181

82-
To fix it, we need to double backslashes, because string quotes turn `\\` into `\`:
82+
Per correggerla, dobbiamo inserire i backslash doppi, poiché gli apici della stringa trasformeranno `\\` in `\`:
8383

8484
```js run
8585
*!*
8686
let regStr = "\\d\\.\\d";
8787
*/!*
88-
alert(regStr); // \d\.\d (correct now)
88+
alert(regStr); // \d\.\d (ora funziona correttamente)
8989

9090
let regexp = new RegExp(regStr);
9191

9292
alert( "Chapter 5.1".match(regexp) ); // 5.1
9393
```
9494

95-
## Summary
95+
## Riepilogo
9696

97-
- To search for special characters `pattern:[ \ ^ $ . | ? * + ( )` literally, we need to prepend them with a backslash `\` ("escape them").
98-
- We also need to escape `/` if we're inside `pattern:/.../` (but not inside `new RegExp`).
99-
- When passing a string to `new RegExp`, we need to double backslashes `\\`, cause string quotes consume one of them.
97+
- Per cercare un carattere speciale `pattern:[ \ ^ $ . | ? * + ( )`, dobbiamo farlo precedere da un backslash `\` (quindi fare l'"escape").
98+
- Dobbiamo anche fare l'escape di `/` se ci troviamo all'interno di `pattern:/.../` (ma non dovremmo farlo dentro a `new RegExp`).
99+
- Quando passiamo una stringa a `new RegExp`, dobbiamo utilizzare il doppio backslash `\\`, poiché gli apici della stringa "consumeranno" uno dei due.

0 commit comments

Comments
 (0)