跳转到主要内容
eLearner.app
模块 4 · 第 4 课(共 4)课程中的16/32~12 min
模块课程(4/4)

命名组和反向引用

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

锻炼#regex.m4.l4.e1
尝试:0加载中...

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

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案

Review exercise

锻炼#regex.m4.l4.e2
尝试:0加载中...

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

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案

Additional challenge

锻炼#regex.m4.l4.e3
尝试:0加载中...

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.

正在加载编辑器...
显示提示

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

3 次尝试后可用的解决方案