Skip to main content
Module 1 · Lesson 1 of 41/32 in the course~15 min
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

ModeFlagsPatternsExample inputsMatches collected
Default (first match, case-sensitive)(none)ciaoCiao, ciao a tutti, ciao.Only 1 match: ciao at index 6
Global (all matches, case-sensitive)gciaoCiao, ciao a tutti, ciao.2 matches: indices 6 and 20
Global + case-insensitivegiciaoCiao, ciao a tutti, ciao.3 matches: Ciao, ciao, ciao
Case-insensitive first occurrence onlyiciaoCiao, ciao a tutti.Only 1 match: Ciao at index 0

Progressive examples

1. Exact literal scan from left to right

Text
Pattern: ciao    (nessuna flag)
Sample:  Buongiorno, ciao mondo! Ti dico anche ciao.
                     ^^^^                        (si ferma qui)

2. Collect all occurrences with the g flag

JS
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

JS
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

Practice Stage

Global case-sensitive literal match

Exercise#regex.m1.l1.e1
Attempts: 0Loading…

Find all exact (case-sensitive) occurrences of the word ciao in the sample by activating the g flag.

Loading editor…
Show hint

The pattern coincides with the word ciao. Do not activate the i flag to exclude Ciao.

Solution available after 3 attempts

Practice Stage

Combining global and case-insensitive search (gi)

Exercise#regex.m1.l1.e2
Attempts: 0Loading…

Find all occurrences of the word errore in text ignoring case.

Loading editor…
Show hint

Use the errore pattern with both the g and i flags active.

Solution available after 3 attempts

Practice Stage

Isolate severity in exactly uppercase in logs

Exercise#regex.m1.l1.e3
Attempts: 0Loading…

Collects all exact occurrences of the tag WARNING (all caps, case-sensitive) ignoring lowercase variants.

Loading editor…
Show hint

Write WARNING in capital letters with only the g flag.

Solution available after 3 attempts

Check for understanding

Regex Quiz#regex.m1.l1.e4
Ready

What happens when running /ok/.test('OK ok OK') versus /ok/g on String.prototype.match('OK ok OK')?

Answer options
Regex Quiz#regex.m1.l1.e5
Ready

What is the output of const re = /ok/g; console.log(re.test('ok'), re.test('ok'));?

Answer options