为了账号安全,请及时绑定邮箱和手机立即绑定

string.replace() 正则有个奇怪的问题。

string.replace() 正则有个奇怪的问题。

蓝山帝景 2018-09-11 13:15:43
// ??? 这样写会发生错误fmt = fmt.replace(/(Y+)/g, date.getFullYear().toString().substr(-RegExp.$1.length));完整代码:'use strict';function formatDate(date,fmt) {    if(!(date instanceof Date)) {        fmt = date;        date = new Date();    }    // fmt = fmt.replace(/(Y+)/g, function() {    //     return date.getFullYear().toString().substr(-RegExp.$1.length);    // });    // ??? 这样写会发生错误    fmt = fmt.replace(/(Y+)/g, date.getFullYear().toString().substr(-RegExp.$1.length));    fmt = fmt.replace(/(M+)/g, date.getMonth() + 1);    fmt = fmt.replace(/(D+)/g, date.getDate());    fmt = fmt.replace(/(h+)/g, date.getHours());    fmt = fmt.replace(/(m+)/g, date.getMinutes());    fmt = fmt.replace(/(s+)/g, date.getSeconds());    fmt = fmt.replace(/(q+)/g, Math.floor((date.getMonth() + 3)/3));    fmt = fmt.replace(/(S+)/g, date.getMilliseconds());    return fmt;}console.log(formatDate('YY-M-D h-m-s S-Q'));
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 874 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号