我编写了如下所示的正则表达式:
/^(\d{4}\-\d\d\-\d\d)|(\d{4}\-\d\d\-\d\d([tT][\d:\.]*){1})([zZ]|([+\-])(\d\d):?(\d\d))?$/.test("1995-01-01")
它工作正常,但即使我给出错误的格式也会返回 true 作为值,请参阅下面的屏幕截图:
最佳答案
试试这个:
const regex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$)/;
console.log(regex.test('1995-01-01')) //true
console.log(regex.test('1995-01-016')) //false
关于javascript - 如何编写正则表达式进行日期格式验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56646408/