Module lessons (1/4)
Literal patterns
In a regex the common alphanumeric characters simply match themselves: write ciao and the engine searches for the exact sequence c followed by i, a, o by scanning from left to right. Each match found always exposes the extracted sub-string and the starting 0-based index.
Mental model: sensitivity to chance and search flags
| Mode | Flags | Patterns | Example inputs | Matches collected |
|---|---|---|---|---|
| Default (first match, case-sensitive) | (none) | ciao | Ciao, ciao a tutti, ciao. | Only 1 match: ciao at index 6 |
| Global (all matches, case-sensitive) | g | ciao | Ciao, ciao a tutti, ciao. | 2 matches: indices 6 and 20 |
| Global + case-insensitive | gi | ciao | Ciao, ciao a tutti, ciao. | 3 matches: Ciao, ciao, ciao |
| Case-insensitive first occurrence only | i | ciao | Ciao, ciao a tutti. | Only 1 match: Ciao at index 0 |
Progressive examples
1. Exact literal scan from left to right
Pattern: ciao (nessuna flag)
Sample: Buongiorno, ciao mondo! Ti dico anche ciao.
^^^^ (si ferma qui)2. Collect all occurrences with the g flag
const logLine = 'INFO boot; ciao Marco; INFO ready; ciao Elena';
const allMatches = logLine.match(/ciao/g);
console.log(allMatches); // ['ciao', 'ciao']3. Ignore case with i and read indexes with matchAll
const rawText = 'ERRORE rete; Errore timeout; errore disco';
const positions = [...rawText.matchAll(/errore/gi)].map((m) => ({
text: m[0],
index: m.index,
}));
// [{ text: 'ERRORE', index: 0 }, { text: 'Errore', index: 13 }, { text: 'errore', index: 29 }]4. Explore live flags g / i and indices on the interactive scanner
Guided laboratory
Global case-sensitive literal match
Find all exact (case-sensitive) occurrences of the word ciao in the sample by activating the g flag.
Show hint
The pattern coincides with the word ciao. Do not activate the i flag to exclude Ciao.
Solution available after 3 attempts
Combining global and case-insensitive search (gi)
Find all occurrences of the word errore in text ignoring case.
Show hint
Use the errore pattern with both the g and i flags active.
Solution available after 3 attempts
Isolate severity in exactly uppercase in logs
Collects all exact occurrences of the tag WARNING (all caps, case-sensitive) ignoring lowercase variants.
Show hint
Write WARNING in capital letters with only the g flag.
Solution available after 3 attempts
Check for understanding
What happens when running /ok/.test('OK ok OK') versus /ok/g on String.prototype.match('OK ok OK')?
What is the output of const re = /ok/g; console.log(re.test('ok'), re.test('ok'));?