const regex = new RegExp('^([+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$', 'gm')
const str = `MATCH
2017
2017-06
2017-06-12
2017-06-12T10Z
2017-06-12T10:10Z
2017-06-12T10:10:15Z
2017-06-12T10:10:15.1Z
2017-06-12T10:10:15.01Z
2017-06-12T10:10:15.001Z
2017-06-12T10:10:15.770Z
2017-06-12T10:10:15Z
2017-06-12T10:10Z
2017-06-12T101015.770Z
2017-06-12T101015Z
2017-06-12T1010Z
20170612T10:10:15.770Z
20170612T10:10:15Z
20170612T10:10Z
20170612T101015.770Z
20170612T101015Z
20170612T1010Z
20170612T10Z
2017-06-12
2017-06-12T
1994-11-05T08:15:30-05:00
1994-11-05T08:15:30-0500
1994-11-05T08:15:30-05
2017-06-12T09:37:45+00:00
2017-06-12T09:37:45Z
20170612T093745Z
DONT MATCH
2017-06-12T24:61:61.770Z
2017-06-12Z
2017-06-12TZ
2017-06-12T101015770Z
2017-06-12T009:307:405Z
2017-06-12T09.37.45Z
2017-0612T10:10:15.770Z
2017-0612T10:10:15Z
2017-0612T10:10Z
2017-0612T101015.770Z
2017-0612T101015Z
2017-0612T1010Z
2017-0612T10Z
201706-12T10:10:15.770Z
201706-12T10:10:15Z
201706-12T10:10Z
201706-12T101015.770Z
201706-12T101015Z
201706-12T1010Z
201706-12T10Z
EXTERNAL
MATCH
2009-12T12:34
2009
2009-05-19
2009-05-19
20090519
2009123
2009-05
2009-123
2009-222
2009-001
2009-W01-1
2009-W51-1
2009-W511
2009-W33
2009W511
2009-05-19
2009-05-19 00:00
2009-05-19 14
2009-05-19 14:31
2009-05-19 14:39:22
2009-05-19T14:39Z
2009-W21-2
2009-W21-2T01:22
2009-139
2009-05-19 14:39:22-06:00
2009-05-19 14:39:22+0600
2009-05-19 14:39:22-01
20090621T0545Z
2007-04-06T00:00
2007-04-05T24:00
2010-02-18T16:23:48.5
2010-02-18T16:23:48,444
2010-02-18T16:23:48,3-06:00
2010-02-18T16:23.4
2010-02-18T16:23,25
2010-02-18T16:23.33+0600
2010-02-18T16.23334444
2010-02-18T16,2283
2009-05-19 143922.500
2009-05-19 1439,55
DONT MATCH
200905
2009367
2009-
2007-04-05T24:50
2009-000
2009-M511
2009M511
2009-05-19T14a39r
2009-05-19T14:3924
2009-0519
2009-05-1914:39
2009-05-19 14:
2009-05-19r14:39
2009-05-19 14a39a22
200912-01
2009-05-19 14:39:22+06a00
2009-05-19 146922.500
2010-02-18T16.5:23.35:48
2010-02-18T16:23.35:48
2010-02-18T16:23.35:48.45
2009-05-19 14.5.44
2010-02-18T16:23.33.600
2010-02-18T16,25:23:48,444`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions