求匹配日期格式的正则表达式String a = "yyyyMMdd"; String b = "yyyy MM dd"; String c = "yyyy-MM-dd"; String d = "yyyy/MM/dd"; String e = "yyyy.MM.dd"; String f = "MM-yyyy-dd"; String g = "dd MM yyyy"; String h = "yyyy-dd-MM"; //匹配上面的格式 //年 月 日 位置变换 还可以匹配的正则~
1 回答
已采纳
一瞬儿光
TA贡献178条经验 获得超70个赞
<script type="text/javascript"> var a = "20161014"; var b = "2016 10 14"; var c = "2016-10-14"; var d = "2016/10/14"; var e = "2016.10.14"; var f = "10-2016-14"; var g = "14 10 2016"; var h = "2016-14-10"; var i = "2016114-10"; function checkDate(date) { var reg = new RegExp("^(((\\d{4})([-|/|\\.|\\s]?\\d{1,2}){2})|((\\d{2})([-|/|\\.|\\s]{1}\\d{4})([-|/|\\.|\\s]{1}\\d{2}))|((\\d{2})([-|/|\\.|\\s]{1}\\d{2})([-|/|\\.|\\s]{1}\\d{4})))$"); return date.match(reg); } console.log(checkDate(a)); console.log(checkDate(b)); console.log(checkDate(c)); console.log(checkDate(d)); console.log(checkDate(e)); console.log(checkDate(f)); console.log(checkDate(g)); console.log(checkDate(h)); console.log(checkDate(i)); </script>
添加回答
举报
0/150
提交
取消