Named Capture Group (Date) in JS
Demonstrate named capture groups by extracting year/month/day from ISO-style dates.
Try it in the JS tester →Pattern
regexJS
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})", "g");
const input = "Born 1990-05-21, hired 2018-09-10";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
(?<year>\d{4}) captures four digits under the name 'year'; (?<month>\d{2}) and (?<day>\d{2}) likewise. Modern JS, Python 3.7+, and Go (with `(?P<...>)` syntax — JS-style is also accepted in Python 3.12+) all support this; the matched substring is accessible by name in code (e.g. m.groups.year in JS, m['year'] in Python).
Examples
Input
Born 1990-05-21, hired 2018-09-10Matches
1990-05-212018-09-10
Input
Today is 2026-04-25Matches
2026-04-25
Input
no dates hereNo match
—