I came up with the following PCRE regex:
[12]\d{3}(-)?(?(1)(0[1-9]|1[0-2]))(-)?(?(3)(0[1-9]|[12]\d|3[01]))
It matches the following date formats:
- 2018-10-03
- 2011-03
- 2003
As you can see, I'm using conditional statements to detect whether there's a dash indicating a next part of the date. Unfortunately, javascript doesn't support conditional statements. How can I convert this to RegExp? I've tried using non-capturing groups containing alternatives, but I didn't manage to do it.
EDIT:
I can't use any JS functions because the regex will be used in the pattern
HTML5 input property.