RegExp named capture groups

/*Without named capture groups*/ var re = /(\d{4})-(\d{2})-(\d{2})/; var match = re.exec('2021-07-09'); console.log(match[1]) // "2021" console.log(match[2]) // "07" console.log(match[3]) // "09" /*With named capture groups*/ var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; var match = re.exec('2021-07-09'); console.log(match.groups.year) // "2021" console.log(match.groups.month) // "07" console.log(match.groups.day) // "09" /*Without destructuring assignment*/ var re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; var [match, year, month, day] = re.exec('2021-07-09'); console.log(match) // "2021-07-09" console.log(year) // "2021" console.log(month) // "07" console.log(day) // "09"
RegExp named capture groups

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.