GeQin

导航

url参数解析 and 日期格式化

~function (pro) {
    //url解析
    function queryURLParameter() {
        var reg = /([^?&=#]+)=([^?&=#]+)/g,
            obj = {};
        this.replace(reg, function () {
            obj[arguments[1]] = arguments[2];
        });
        reg = /#([^?=&#]+)/;
        this.replace(reg, function () {
            obj['HASH'] = arguments[1];
        });
        return obj;
    }
    //日期格式化
    function formatDate(temp) {
        temp = temp || '{0}年{1}月{2}日 {3}时{4}分{5}秒';
        var ary = this.match(/\d+/g);
        temp = temp.replace(/\{(\d+)\}/g, function () {
            var index = arguments[1],
                item = parseFloat(ary[index]) || 0;
            item < 10 ? item = '0' + item : null;
            return item;
        });
        return temp;
    }

    pro.queryURLParameter = queryURLParameter;
    pro.formatDate = formatDate;
}(String.prototype);

//用法
var urlStr= 'http://localhost:63342/project_help/help_v2/detail.html?helpUserId=111&publishId=7777&helpType=3';
var strObj = urlStr.queryURLParameter();
//{helpUserId: "111", publishId: "7777", helpType: "3"}

  

posted on 2019-01-18 17:03  GeQin  阅读(323)  评论(0编辑  收藏  举报