Is there any way to have a regex in JavaScript that validates dates of multiple formats, like: DD-MM-YYYY or DD.MM.YYYY or DD/MM/YYYY etc? I need all these in one regex and I’m not really good with it. So far I’ve come up with this: var dateReg = /^\d{2}-\d{2}-\d{4}$/; for DD-MM-YYYY. I only need to validate the date format, not the date itself.
Is there any way to have a regex in JavaScript that validates dates of
Share
You could use a character class (
[./-]) so that the seperators can be any of the defined charactersOr better still, match the character class for the first seperator, then capture that as a group
([./-])and use a reference to the captured group\1to match the second seperator, which will ensure that both seperators are the same: