1.去除字符串空格
/*去左空格*/ function ltrim(s) { return s.replace(/^(s*| *)/, ""); } /*去右空格*/ function rtrim(s) { return s.replace(/(s*| *)$/, ""); } /*去左右空格*/ function trim(s) { return ltrim(rtrim(s)); }
2.获取url地址栏参数
function getUrlQueryParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); var value = ''; if (r != null) { value = decodeURIComponent(r[2]); } return value; }
3.数字千分位转换
/*数字格式转换成千分位*/ function numToMicrometer(num) { if (trim(num + "") == "") { return ""; } if (isNaN(num)) { return ""; } num = num + ""; if (/^.*..*$/.test(num)) { varpointIndex = num.lastIndexOf("."); varintPart = num.substring(0, pointIndex); varpointPart = num.substring(pointIndex + 1, num.length); intPart = intPart + ""; var re = /(-?d+)(d{3})/ while (re.test(intPart)) { intPart = intPart.replace(re, "$1,$2") } num = intPart + "." + pointPart; } else { num = num + ""; var re = /(-?d+)(d{3})/ while (re.test(num)) { num = num.replace(re, "$1,$2") } } return num; } /*去除千分位*/ function delMicrometer(num) { if (trim(num + "") == "") { return ""; } num = num.replace(/,/gi, ''); return num; }
4.获取给定字符串占用的字节长度
function getByteLen(val) { // 传入一个字符串 var len = 0; for (var i = 0; i < val.length; i++) { if (val[i].match(/[^x00-xff]/ig) != null) // 全角 len += 2; // 如果是全角,占用两个字节 else len += 1; // 半角占用一个字节 } return len; }
5.构建url参数
/* param = { name1 : value1, name2 : value2, ... } */ function buildParam(param) { var paramContent = ''; try { if (!empty(param)) { for (var key in param) { paramContent += key + '=' + encodeURIComponent(param[key]) + '&'; } } } catch (e) { } return paramContent; }
6.判断变量是否空值 undefined, null, '', false, 0, [], {} 均返回true,否则返回false
function empty(v) { switch (typeof v) { case 'undefined' : return true; case 'string' : if (trim(v).length == 0) return true; break; case 'boolean' : if (!v) return true; break; case 'number' : if (0 === v) return true; break; case 'object' : if (null === v) return true; if (undefined !== v.length && v.length == 0) return true; for (var k in v) { return false; } return true; break; } return false; }
7.去除html标签
/*去除html标记*/ function removeHtmlTag(str) { return str.replace(/<[^>]+>/g, ""); }
8.获取url参数(返回数组类型)
function getRequestParam(param) { var url = location.search; // 获取url中"?"符后的字串 var paramStr = new Array(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { if (param == strs[i].split("=")[0]) { paramStr.push(strs[i].split("=")[1]); } } } return paramStr; }
9.指定字符高亮
function setHighLightText(txt, currentTargetText) { if (empty(txt)) { return ''; } var newTxt = txt; if (currentTargetText.length != 0) { $(currentTargetText).each(function(i) { var reg = new RegExp(currentTargetText[i], "g"); newTxt = newTxt.replace(reg, "<em>" + currentTargetText[i] + "</em>"); }); } return newTxt; }
10.替换url中的指定参数
function changeURLArg(url, paramName, paramName_val) { var pattern = paramName + '=([^&]*)'; var replaceText = paramName + '=' + paramName_val; if (url.match(pattern)) { var tmp = '/(' + paramName + '=)([^&]*)/gi'; tmp = url.replace(eval(tmp), replaceText); return tmp; } else { if (url.match('[?]')) { return url + '&' + replaceText; } else { return url + '?' + replaceText; } } return url + ' ' + paramName + ' ' + paramName_val; }