正则表达式替换日期和时间
Regex to replace date and hour
谁能帮我做这个面具,日期部分没问题,但我不知道如何实现小时?
正则表达式是:(\d{2}\/)+(\d{4})\s*(\d{2}:\d{2})/
const onMask = (v, mask) => {
if (!v) return;
v = v.replace(/\D/g,'');
switch(mask) {
case 'datehour':
v = v.replace(/(\d{2})(\d)/, '/');
v = v.replace(/(\d{2})(\d)/, '/');
v = v.replace(/(\d{4})(\d)/, ' ');
break;
}
使用类似
的东西
var v = '23-12-2021 12-34'
v = v.replace(/\D+/g, '');
v = v.replace(/^(\d{2})(\d)/, '/');
v = v.replace(/^(\d{2}\/\d{2})(\d)/, '/');
v = v.replace(/^(\d{2}\/\d{2}\/\d{4})(\d)/, ' ');
v = v.replace(/^(\d{2}\/\d{2}\/\d{4} \d{2})(\d)/, ':');
console.log(v)
在重新格式化数据之前删除非数字字符,在预期位置添加 space/slash/colon。
谁能帮我做这个面具,日期部分没问题,但我不知道如何实现小时?
正则表达式是:(\d{2}\/)+(\d{4})\s*(\d{2}:\d{2})/
const onMask = (v, mask) => {
if (!v) return;
v = v.replace(/\D/g,'');
switch(mask) {
case 'datehour':
v = v.replace(/(\d{2})(\d)/, '/');
v = v.replace(/(\d{2})(\d)/, '/');
v = v.replace(/(\d{4})(\d)/, ' ');
break;
}
使用类似
的东西var v = '23-12-2021 12-34'
v = v.replace(/\D+/g, '');
v = v.replace(/^(\d{2})(\d)/, '/');
v = v.replace(/^(\d{2}\/\d{2})(\d)/, '/');
v = v.replace(/^(\d{2}\/\d{2}\/\d{4})(\d)/, ' ');
v = v.replace(/^(\d{2}\/\d{2}\/\d{4} \d{2})(\d)/, ':');
console.log(v)
在重新格式化数据之前删除非数字字符,在预期位置添加 space/slash/colon。