Ana içeriğe geç
eLearner.app
Modül 4 · Ders 4 ders 4Kurstaki 16/32~12 min
Modül dersleri (4/4)

Adlandırılmış gruplar ve geri referanslar

When the number of groups grows, remembering that $2 is "the month" and $5 is "the year" becomes fragile. The (?<name>...) syntax lets you name a group: you access it by name via groups.name (and in replacements as $<name>).

Code
Pattern: (?<anno>\d{4})-(?<mese>\d{2})-(?<giorno>\d{2})
Sample:  2024-03-15
Gruppi:
  anno    = "2024"
  mese    = "03"
  giorno  = "15"

The editor shows the named groups next to the match.

Backreference

A backreference lets you refer, inside the same pattern, to a previous capture. The syntax:

  • \1, \2, … -- numbered reference (to group 1, 2, …).
  • \k<name> -- named reference.
Code
Pattern: \b(\w+)\s+\1\b
Sample:  un test un test il il gatto
                                ^^^^^

It matches consecutive duplicate words: (\w+) captures a word, then \s+\1 requires a space and the same word.

Self-documenting code and named back-references

Named groups improve the readability of JavaScript code: instead of referring to match[1], you access match.groups.name. Inside the pattern itself, referencing them is done with the \\k<name> syntax.

Try it

Egzersiz#regex.m4.l4.e1
Denemeler: 0Yükleniyor…

Find every date in YYYY-MM-DD format naming the groups `anno`, `mese`, `giorno`.

Düzenleyici yükleniyor…
İpucu göster

Syntax: (?<name>pattern). The match shows the groups under the 'named' entry.

3 denemeden sonra çözüm mevcut

Review exercise

Egzersiz#regex.m4.l4.e2
Denemeler: 0Yükleniyor…

Find consecutive duplicate words (e.g. `il il`, `casa casa`). Use a group for the first one and a backreference `\\1` for the second.

Düzenleyici yükleniyor…
İpucu göster

\\b(\\w+)\\s+\\1\\b: the first word is (\\w+), then a space, then the SAME word \\1.

3 denemeden sonra çözüm mevcut

Additional challenge

Egzersiz#regex.m4.l4.e3
Denemeler: 0Yükleniyor…

Find HTML/XML tag pairs (e.g. `<b>text</b>` or `<i>text</i>`) ensuring that the closing tag matches the opening tag using a named backreference.

Düzenleyici yükleniyor…
İpucu göster

Assign the name (?<tag>\w+) and close it with \k<tag>.

3 denemeden sonra çözüm mevcut