码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-01-09 22:55:48      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

/*特殊字符转义*/
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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/zzp5980/p/5117386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!