I have a RegEx that matches a Date in the formats documented:
/**
* RegExp to test for a date in YYYY-MM-DD format (with or without separators)
* where year has to be 1900-9999, MM = 01-12, DD = 01 - 31
* YYYYMMDD
* YYYY-MM-DD
* YYYY/MM/DD
* These also match but I'm not thrilled with it
* YYYY-MM/DD
* YYYY/MMDD (also matches YYYY-MMDD)
* YYYYMM-DD (also matches YYYYMM/DD)
* @type {RegExp}
*/
const patternYYYYMMDD = /\d{4}[-\/]?(0[1-9]|1[0-2])[-\/]?(0[1-9]|[12][0-9]|3[01])/;
// To test (open browser console and paste)
const p = (msg) => console.log(msg);
p(patternYYYYMMDD.test("20220102"))
p(patternYYYYMMDD.test("2022-01-02"))
p(patternYYYYMMDD.test("2022/01/02"))
p(patternYYYYMMDD.test("2022/0102"))
p(patternYYYYMMDD.test("202201/02"))
p(patternYYYYMMDD.test("2022/01-02"))
My question is: Is it possible to create a RegEx that remembers the first separator used and only allow that separator and to require the separator?
When searching for an answer, this was very helpful. https://stackoverflow.com/a/37563868/3281336
This might be the answer, I just don't understand how to apply the information in this answer. https://stackoverflow.com/a/17949129/3281336