| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /**
- * 判断字符是否为空
- */
- 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, '<').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 '<span class="' + cls + '">' + match + '</span>';
- });
- }
- // 数字正则式
- var numReg = /-?[1-9]\d*/;
- /**
- * 高亮关键字 text =>内容 words:关键词 tag 被包裹的标签
- * <pre>
- * 匹配每一个关键字字符
- * </pre>
- * @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">$&</'+ tag +'>');
- }
- }
- 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">$&</' + tag + '>');
- }
- return text;
- }
|