const re = /(\d{4})-(\d{2})-(\d{2})/
const match= re.exec('2021-07-12')
console.log(match[0]) // 2021-07-12
console.log(match[1]) // 2021
console.log(match[2]) // 07
console.log(match[3]) // 12
ES9 引入了命名捕获组,允许为每一个组匹配指定一个名字,既便于阅读代码,又便于引用
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const match = re.exec('2021-07-12')
console.log(match.groups) // {year: "2021", month: "07", day: "12"}
console.log(match.groups.year) // 2021
console.log(match.groups.month) // 07
console.log(match.groups.day) // 12