1.6 KiB
1.6 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db4367417b2b2512b92 | Estrarre le corrispondenze | 1 | 301340 | extract-matches |
--description--
Finora, hai solo verificato se un pattern esiste o meno all'interno di una stringa. Puoi anche estrarre le corrispondenze che hai trovato con il metodo .match()
.
Per usare il metodo .match()
, applica il metodo su una stringa e passa l'espressione regolare tra parentesi.
Ecco un esempio:
"Hello, World!".match(/Hello/);
let ourStr = "Regular expressions";
let ourRegex = /expressions/;
ourStr.match(ourRegex);
Qui la prima corrispondenza (match
) restituirà ["Hello"]
e la seconda restituirà ["expressions"]
.
Nota che la sintassi di .match
è il "contrario" del metodo .test
che hai utilizzato finora:
'string'.match(/regex/);
/regex/.test('string');
--instructions--
Applica il metodo .match()
per estrarre la stringa coding
.
--hints--
Il risultato (result
) dovrebbe contenere la stringa coding
assert(result.join() === 'coding');
La tua espressione regolare codingRegex
dovrebbe cercare la stringa coding
assert(codingRegex.source === 'coding');
Dovresti usare il metodo .match()
.
assert(code.match(/\.match\(.*\)/));
--seed--
--seed-contents--
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /change/; // Change this line
let result = extractStr; // Change this line
--solutions--
let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line