/** * 判断字符是否为空 */ function isEmpty(exp) { if (null == exp || typeof exp == "null" || typeof(exp) == "undefined" || !exp || "" == exp) { return true; } return false; } /** * 时间转换 * * @param inputTime * @returns {string} */ function formatDateTime(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second; }; /** * json着色 * * @param json * @returns {XML|string} */ function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); } json = json.replace(/&/g, '&').replace(//g, '>'); return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) { var cls = 'number'; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = 'key'; } else { cls = 'string'; } } else if (/true|false/.test(match)) { cls = 'boolean'; } else if (/null/.test(match)) { cls = 'null'; } return '' + match + ''; }); } // 数字正则式 var numReg = /-?[1-9]\d*/; /** * 高亮关键字 text =>内容 words:关键词 tag 被包裹的标签 *
 *     匹配每一个关键字字符
 * 
* @param text * @param words * @param tag * @returns {*} */ function highlight(text, words, tag) { // 默认的标签,如果没有指定,使用span tag = tag || 'span'; if (numReg.test(text)) { text = text.toString(); } var i, len = words.length, re; for (i = 0; i < len; i++) { // 正则匹配所有的文本 re = new RegExp(words[i], 'g'); if (re.test(text)) { text = text.replace(re, '<'+ tag +' class="highlight">$&'); } } return text; } /** * 匹配整个关键词 不拆分 * * @param text * @param words * @param tag * @returns {*} */ function highLightKeywords(text, words, tag) { // 默认的标签,如果没有指定,使用span tag = tag || 'span'; if (numReg.test(text)) { text = text.toString(); } var i, len = words.length, re; //匹配每一个特殊字符 ,进行转义 var specialStr = ["*", ".", "?", "+", "$", "^", "[", "]", "{", "}", "|", "\\", "(", ")", "/", "%"]; $.each(specialStr, function(i, item) { if(words.indexOf(item) != -1) { words = words.replace(new RegExp("\\" + item, 'g'), "\\" + item); } }); //匹配整个关键词 re = new RegExp(words, 'g'); if (re.test(text)) { text = text.replace(re, '<' + tag + ' class="highlight">$&'); } return text; }