Regexdrill

Level 5: Advanced Exercise 68 of 70 Free

Named groups in a replacement

A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.

The task

Rewrite every date written YYYY-MM-DD as DD.MM.YYYY. Capture the parts in named groups and refer to them by name in the replacement. Leave everything else unchanged.

What this exercise teaches

(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2}) with the replacement $<d>.$<m>.$<y> and the g flag reorders every date. Named references still read correctly when groups are moved or added, which $1 and $2 do not. Without named groups in the pattern, $<d> is inserted as literal text.

A hint if you get stuck

In a JavaScript replacement string, $<name> inserts the group called name.

Try it now, free, no account needed

Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.

Open this exercise

More exercises in Advanced