Created
December 1, 2018 14:46
-
-
Save talitaoliveira/c124487d7382bd4bca4b726120cc0d3f to your computer and use it in GitHub Desktop.
Using the new feature of ES9: Regexp named group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RE_DATE = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/; | |
const matchObj = RE_DATE.exec('2018-12-01'); | |
console.log(matchObj); | |
/** | |
* [ | |
* 0: "2018-12-01", | |
* 1: "2018", | |
* 2: "12", | |
* 3: "01", | |
* groups: { | |
* year: 2018, | |
* month: 12, | |
* day: 01, | |
* }, | |
* input: "2018-12-01" | |
* ] | |
*/ | |
const year = matchObj.groups.year; // 2018 | |
const month = matchObj.groups.month; // 12 | |
const day = matchObj.groups.day; // 01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment