base.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * 判断字符是否为空
  3. */
  4. function isEmpty(exp) {
  5. if (null == exp || typeof exp == "null" || typeof(exp) == "undefined" || !exp || "" == exp) {
  6. return true;
  7. }
  8. return false;
  9. }
  10. /**
  11. * 时间转换
  12. *
  13. * @param inputTime
  14. * @returns {string}
  15. */
  16. function formatDateTime(inputTime) {
  17. var date = new Date(inputTime);
  18. var y = date.getFullYear();
  19. var m = date.getMonth() + 1;
  20. m = m < 10 ? ('0' + m) : m;
  21. var d = date.getDate();
  22. d = d < 10 ? ('0' + d) : d;
  23. var h = date.getHours();
  24. h = h < 10 ? ('0' + h) : h;
  25. var minute = date.getMinutes();
  26. var second = date.getSeconds();
  27. minute = minute < 10 ? ('0' + minute) : minute;
  28. second = second < 10 ? ('0' + second) : second;
  29. return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
  30. };
  31. /**
  32. * json着色
  33. *
  34. * @param json
  35. * @returns {XML|string}
  36. */
  37. function syntaxHighlight(json) {
  38. if (typeof json != 'string') {
  39. json = JSON.stringify(json, undefined, 2);
  40. }
  41. json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
  42. return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
  43. var cls = 'number';
  44. if (/^"/.test(match)) {
  45. if (/:$/.test(match)) {
  46. cls = 'key';
  47. } else {
  48. cls = 'string';
  49. }
  50. } else if (/true|false/.test(match)) {
  51. cls = 'boolean';
  52. } else if (/null/.test(match)) {
  53. cls = 'null';
  54. }
  55. return '<span class="' + cls + '">' + match + '</span>';
  56. });
  57. }
  58. // 数字正则式
  59. var numReg = /-?[1-9]\d*/;
  60. /**
  61. * 高亮关键字 text =>内容 words:关键词 tag 被包裹的标签
  62. * <pre>
  63. * 匹配每一个关键字字符
  64. * </pre>
  65. * @param text
  66. * @param words
  67. * @param tag
  68. * @returns {*}
  69. */
  70. function highlight(text, words, tag) {
  71. // 默认的标签,如果没有指定,使用span
  72. tag = tag || 'span';
  73. if (numReg.test(text)) {
  74. text = text.toString();
  75. }
  76. var i, len = words.length, re;
  77. for (i = 0; i < len; i++) {
  78. // 正则匹配所有的文本
  79. re = new RegExp(words[i], 'g');
  80. if (re.test(text)) {
  81. text = text.replace(re, '<'+ tag +' class="highlight">$&</'+ tag +'>');
  82. }
  83. }
  84. return text;
  85. }
  86. /**
  87. * 匹配整个关键词 不拆分
  88. *
  89. * @param text
  90. * @param words
  91. * @param tag
  92. * @returns {*}
  93. */
  94. function highLightKeywords(text, words, tag) {
  95. // 默认的标签,如果没有指定,使用span
  96. tag = tag || 'span';
  97. if (numReg.test(text)) {
  98. text = text.toString();
  99. }
  100. var i, len = words.length, re;
  101. //匹配每一个特殊字符 ,进行转义
  102. var specialStr = ["*", ".", "?", "+", "$", "^", "[", "]", "{", "}", "|", "\\", "(", ")", "/", "%"];
  103. $.each(specialStr, function(i, item) {
  104. if(words.indexOf(item) != -1) {
  105. words = words.replace(new RegExp("\\" + item, 'g'), "\\" + item);
  106. }
  107. });
  108. //匹配整个关键词
  109. re = new RegExp(words, 'g');
  110. if (re.test(text)) {
  111. text = text.replace(re, '<' + tag + ' class="highlight">$&</' + tag + '>');
  112. }
  113. return text;
  114. }