diff --git a/9-regular-expressions/01-regexp-introduction/article.md b/9-regular-expressions/01-regexp-introduction/article.md index 57fe02f5d..33ed8e15c 100644 --- a/9-regular-expressions/01-regexp-introduction/article.md +++ b/9-regular-expressions/01-regexp-introduction/article.md @@ -1,119 +1,119 @@ -# Patterns and flags +# Pattern e flag -A regular expression (also "regexp", or just "reg") consists of a *pattern* and optional *flags*. +Una espressione regolare (regular expression, "regexp" o solo "reg") è formata da una sequenza di caratteri (*pattern*) e da eventuali *flag*. -There are two syntaxes to create a regular expression object. +Esistono due tipi di sintassi per creare un oggetto di tipo "regular expression". -The long syntax: +La versione più lunga: ```js regexp = new RegExp("pattern", "flags"); ``` -...And the short one, using slashes `"/"`: +...e la più corta, usando gli slash `"/"`: ```js -regexp = /pattern/; // no flags -regexp = /pattern/gmi; // with flags g,m and i (to be covered soon) +regexp = /pattern/; // senza flag +regexp = /pattern/gmi; // con le flag g,m e i (da approfondire a breve) ``` -Slashes `"/"` tell JavaScript that we are creating a regular expression. They play the same role as quotes for strings. +Gli slash `"/"` comunicano a JavaScript che stiamo creando una espressione regolare. Hanno lo stesso ruolo delle virgolette per le stringhe. -## Usage +## Uso -To search inside a string, we can use method [search](mdn:js/String/search). +Per cercare in una stringa, possiamo usare il metodo [search](mdn:js/String/search). -Here's an example: +Qui un esempio: ```js run -let str = "I love JavaScript!"; // will search here +let str = "I love Javascript!"; // cercherà qui let regexp = /love/; alert( str.search(regexp) ); // 2 ``` -The `str.search` method looks for the pattern `pattern:/love/` and returns the position inside the string. As we might guess, `pattern:/love/` is the simplest possible pattern. What it does is a simple substring search. +Il metodo `str.search` cerca la sequenza `pattern:/love/` e restituisce la sua posizione all'interno della stringa. Come si può immaginare, il pattern `pattern:/love/` è la sequenza più semplice possibile. Quel che fa è la semplice ricerca di una sottostringa. -The code above is the same as: +Il codice qui sopra fa lo stesso di: ```js run -let str = "I love JavaScript!"; // will search here +let str = "I love JavaScript!"; // cercherà qui let substr = 'love'; alert( str.search(substr) ); // 2 ``` -So searching for `pattern:/love/` is the same as searching for `"love"`. +Quindi se cerchiamo la sequenza `pattern:/love/` avremo lo stesso risultato che otteniamo cercando `"love"`. -But that's only for now. Soon we'll create more complex regular expressions with much more searching power. +Questo vale solo per il momento. Presto creeremo espressioni regolari più complesse e con maggiore potere di ricerca. ```smart header="Colors" -From here on the color scheme is: +Da qui in avanti, lo schema di colori è il seguente: -- regexp -- `pattern:red` -- string (where we search) -- `subject:blue` -- result -- `match:green` +- espressione regolare -- `pattern:rosso` +- stringa (all'interno della quale cerchiamo) -- `subject:blu` +- risultato -- `match:verde` ``` -````smart header="When to use `new RegExp`?" -Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`. +Quando usiamo la sintassi `new RegExp`? +Usualmente usiamo la sintassi più breve `/.../`. Ma non supporta inserimenti di variabli con `${...}`. -On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible. +Tuttavia, `new RegExp` permette di costruire dinamicamente una sequenza da una stringa, quindi è più flessibile. -Here's an example of a dynamically generated regexp: +Qui un esempio di un'espressione regolare generata dinamicamente: ```js run -let tag = prompt("Which tag you want to search?", "h2"); +let tag = prompt("Quale tag vuoi cercare?", "h2"); let regexp = new RegExp(`<${tag}>`); -// finds