1.0.2 • Published 5 years ago
calendar-date-regex v1.0.2
calendar-date-regex
A regular expression for matching ISO 8601 calendar dates
'2015-01-25 0101 20150126 11112233 2015-13-13'.match(calendarDateRegex());
//=> ['2015-01-25' '20150126']
Installation
npm install calendar-date-regex
API
import calendarDateRegex from 'calendar-date-regex';
calendarDateRegex(options)
options: Object
Return: RegExp
It returns a regular expression object that matches calendar dates (YYYY-MM-DD and YYYYMMDD).
calendarDateRegex().exec(' 16140526 ').slice();
//=> ['16140526', '1614', '05', '26', undefined, undefined, undefined]
calendarDateRegex().exec(' 1614-05-26 ').slice();
//=> ['1614-05-26', undefined, undefined, undefined, '1614', '05', '26']
options.basic
Type: boolean
Default: true
false
prevents the regex from matching the basic format (YYYYMMDD).
calendarDateRegex({basic: false}).test('20130413'); //=> false
options.extended
Type: boolean
Default: true
false
prevents the regex from matching the extended format (YYYY-MM-DD).
calendarDateRegex({extended: false}).test('2013-04-13'); //=> false
options.exact
Type: boolean
Default: false
true
makes the regex matches only an exact calendar date string.
calendarDateRegex().test('foo 11900331'); //=> true
calendarDateRegex({exact: true}).test('foo 11900331'); //=> false
calendarDateRegex({basic: false, exact: true}).test('11900331'); //=> false
calendarDateRegex.noDay(options)
options: Object
(only supports exact
option)
Return: RegExp
It returns a regular expression object that matches calendar dates with reduced precision (YYYY-MM).
calendarDateRegex.noDay().test('2015-01'); //=> true
calendarDateRegex.noDay().test('201501'); //=> false
License
Copyright (c) 2015 - 2018 Shinnosuke Watanabe
Licensed under the MIT License.