/** * Created by guojie on 2015/1/24. * 静态工具类 */ var Utils = {}; //处理字符串 Utils.String = { //修剪两端空白字符和换行符 trim:function (s){ return s.replace(/(^\s*)|(\s*$)|(\n)/g, ""); }, //修剪左端空白字符和换行符 leftTrim:function (s){ return s.replace(/(^\s*)|(^\n)/g, ""); }, //修剪右端空白字符和换行符 rightTrim:function (s){ return s.replace(/(\s*$)|(\n$)/g, ""); }, // 是否是URL isUrl: function(s) { //var p = /^((https|http|ftp|rtsp|mms)?:\/\/)(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]$)|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,6})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/; var p = /^((https|http|ftp|rtsp|mms)?:\/\/)[A-Za-z0-9-]+\.[A-Za-z0-9-]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/; s = s.toLowerCase(); return p.test(s); }, //格式化数字 numberFormat:function (s,l){ if (!l || l < 1)l = 3; s=String(s).split("."); s[0]=s[0].replace(new RegExp('(\\d)(?=(\\d{'+l+'})+$)','ig'),"$1,"); return s.join("."); }, //字母类型检测 isString:function (s){ var patrn=/^([a-z]|[A-Z])+$/; return patrn.exec(s); }, //浮点数类型检测 isNumber:function (s){ var patrn=/^\d+\.\d+$/; return patrn.exec(s); }, //整数类型检测 isInt:function (s){ var patrn=/^-?\d+$/; return patrn.exec(s); }, //正整数类型检测 isUint:function (s){ var patrn=/^\d+$/; return patrn.exec(s); }, //星号字节 asteriskByte:function(s,start,end) { var startStr = start?s.substr(0,start):""; var endStr = end?s.substr(end+1):""; var star = "",l; l = !start && !end?s.length:(start && !end?s.length - start:end); while(star.length < l)star += "*"; return startStr + star + endStr; }, //四舍五入保留n位小数(默认保留两位小数) twoDecimalPlaces:function(s,l) { if (isNaN(parseFloat(s))||s==0) return "0.00"; var bit = !l?100:Math.pow(10,l); var str = String(Math.round(s * bit) / bit); while (str.indexOf(".") != -1 && str.length <= str.indexOf(".") + l)str += '0'; return str; } }; //获取地址栏参数 Utils.getQueryString = function(attr) { var parameter = window.location.search; var reg = new RegExp(attr+"=([^&]*)") var result = parameter.substr(1).match(reg); return (result!=null&&result.length>1)?result[1]:null; } //滚动到页面底部 Utils.isScrollBottom = function() { var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; var clientHeight = document.documentElement.clientHeight; if(scrollTop + clientHeight >= document.body.scrollHeight) return true; return false; } //设置cookie Utils.setCookie = function(name,value,iDay) { //当前时间和过期时间相加 var date = new Date(); date.setDate(date.getDate()+iDay); //给cookie赋值 document.cookie = name+"="+value+";expires="+date; } //获取cookie Utils.getCookie = function(name) { //cookie转换成数组 var arr = document.cookie.split("; "); for(var i = 0; i