js对特殊字符转义、时间格式化、获取URL参数

/*特殊字符转义*/
function replace_html(str) {
var str = str.toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, '&quot;');
return str;
}
/*
*时间格式化
*例子:time = new Date().Format("yyyy-MM-dd hh:mm:ss");
*/
Date.prototype.Format = function (result) {
var o = {
"M+": this.getMonth() + 1, //month
"D+": this.getDate(), //day
"h+": this.getHours(), //hours
"m+": this.getMinutes(), //minutes
"s+": this.getSeconds(), //seconds
"q+": Math.floor((this.getMonth() + 3) / 3), // season
"S": this.getMilliseconds() //milliseconds
};
if (/(Y+)/.test(result)) {
result = result.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o){
if (new RegExp("(" + k + ")").test(result)) {
result = result.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return result;
};


/*
*获取URL参数
*/
function getQueryString(key){
var reg = new RegExp("(^|&)"+ key +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null){
return unescape(r[2]);
}
return null;
}

 

posted @ 2016-01-09 21:57  zzp5980  阅读(1243)  评论(0编辑  收藏  举报