完全匹配(首位0要求保留):
let reg = /^([1-2]\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|30|31) (0\d|1\d|2[0-4]):(0\d|[1-5]\d):(0\d|[1-5]\d)$/
let str = "2018-10-20 10:00:00";
console.log(str.match(reg))
console.log(reg.test(str))
首位0可有可没有:
let reg = /^([1-2]\d{3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|30|31) (0?\d|1\d|2[0-4]):(0?\d|[1-5]\d):(0?\d|[1-5]\d)$/
let str = "2018-10-20 10:0:00";
console.log(str.match(reg))
console.log(reg.test(str))