Skip to content

Instantly share code, notes, and snippets.

@talitaoliveira
Created December 1, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talitaoliveira/c124487d7382bd4bca4b726120cc0d3f to your computer and use it in GitHub Desktop.
Save talitaoliveira/c124487d7382bd4bca4b726120cc0d3f to your computer and use it in GitHub Desktop.
Using the new feature of ES9: Regexp named group
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