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 exerciseMore exercises in Advanced
- Lookahead: amounts before USDFree
- Negative lookahead: not starting with unFree
- Lookbehind: amounts after $Free
- Negative lookbehind: positive numbers onlyFree
- Lazy vs greedy: the first quoted textFree
- Named groups: year, month, dayFree
- Password rules with lookaheadsFree
- Word boundaries: replace a whole wordFree
- Unicode letters with the u flagFree
- Multiline flag: TODO linesFree
- Backreference across lines: duplicate linesFree
- Multiline: trailing whitespace on every lineFree
- Multiline anchors: lines ending in a semicolonFree
- Negative lookahead: numbers that are not percentagesFree