日期正则表达式 yyyy-MM-dd,yyyy/MM/dd,MM/dd/yyyy,一个正则表达式可以验证这三种还有一种格式是yyyyMMdd
2 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
var a = '2011-12-21';
var b = '2011/01/21';
var c = '20141230';
var d = '12/21/2011'; function test(s){ var r1 = /^(\d{2})\/(\d{2})\/(\d{4})$/; if(r1.test(s)){ s = RegExp.$3+'-'+RegExp.$1+'-'+RegExp.$2; return Boolean(+new Date(s)); }else{ var r1 = /^(\d{4})([-/]?)(\d{2})\2(\d{2})$/; if(r1.test(s)){ s = RegExp.$1+'-'+RegExp.$3+'-'+RegExp.$4; return Boolean(+new Date(s)); }else{ return false; } } } console.log(test(a)); console.log(test(b)); console.log(test(c)); console.log(test(d));
- 2 回答
- 0 关注
- 723 浏览
添加回答
举报
0/150
提交
取消