index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * FeHelper 正则工具
  3. */
  4. /**
  5. * 自适应高度的jquery插件
  6. */
  7. $.fn.extend({
  8. textareaAutoHeight:function (options) {
  9. this._options = {
  10. minHeight:0,
  11. maxHeight:100000
  12. };
  13. this.init = function () {
  14. for (var p in options) {
  15. this._options[p] = options[p];
  16. }
  17. if (this._options.minHeight === 0) {
  18. this._options.minHeight = parseFloat($(this).height());
  19. }
  20. for (var p in this._options) {
  21. if ($(this).attr(p) == null) {
  22. $(this).attr(p, this._options[p]);
  23. }
  24. }
  25. $(this).keyup(this.resetHeight).change(this.resetHeight)
  26. .focus(this.resetHeight);
  27. };
  28. this.resetHeight = function () {
  29. var _minHeight = parseFloat($(this).attr("minHeight"));
  30. var _maxHeight = parseFloat($(this).attr("maxHeight"));
  31. $(this).height(0);
  32. var h = parseFloat(this.scrollHeight);
  33. h = h < _minHeight ? _minHeight :
  34. h > _maxHeight ? _maxHeight : h;
  35. $(this).height(h).scrollTop(h);
  36. if (h >= _maxHeight) {
  37. $(this).css("overflow-y", "scroll");
  38. }
  39. else {
  40. $(this).css("overflow-y", "hidden");
  41. }
  42. };
  43. this.init();
  44. }
  45. });
  46. var RegExpTools = (function () {
  47. "use strict";
  48. var regElm, srcElm, rstElm, rstCount, srcBackgroundElm, srcWrapperElm, regListElm;
  49. var ID_PREFIX = 'tmp_id_';
  50. var TAG_MATCHED = 'b';
  51. var TAG_NOT_MATCHED = 'i';
  52. var TR_ID_PREFIX = 'tr_' + ID_PREFIX;
  53. var _getRegExp = function (regTxt) {
  54. try {
  55. return new Function('return ' + regTxt)();
  56. } catch (e) {
  57. return null;
  58. }
  59. };
  60. var _buildTable = function (rstArray) {
  61. var tbl = ["<table class='table table-bordered table-striped table-condensed table-hover'>"];
  62. tbl.push('<tr class="active"><th class="num">序号</th><th>匹配结果</th><th>在原字符串中的位置</th></tr>')
  63. $.each(rstArray, function (i, item) {
  64. tbl.push('<tr id="' + TR_ID_PREFIX + item.index + '" data-index="' + item.index + '">');
  65. tbl.push('<td class="num">' + (i + 1) + '</td>'
  66. + '<td class="content">' + item.text + '</td>'
  67. + '<td class="index">' + item.index + '</td>');
  68. tbl.push('</tr>');
  69. });
  70. tbl.push('</table>');
  71. return tbl.join('');
  72. };
  73. var _createTag = function (type, item) {
  74. var tags = [];
  75. for (var i = 0, len = item.text.length; i < len; i++) {
  76. tags.push('<' + type + ' data-id="' + ID_PREFIX + item.index + '">'
  77. + item.text.charAt(i) + '</' + type + '>');
  78. }
  79. return tags.join('');
  80. };
  81. var _blinkHighlight = function () {
  82. $('tr[id^=' + TR_ID_PREFIX + ']').click(function (e) {
  83. var index = $(this).attr('data-index');
  84. var tags = $(TAG_MATCHED + '[data-id=' + ID_PREFIX + index + ']');
  85. tags.animate({
  86. opacity:0
  87. }, 200).delay().animate({
  88. opacity:1
  89. }, 200).delay().animate({
  90. opacity:0
  91. }, 200).delay().animate({
  92. opacity:1
  93. }, 200);
  94. });
  95. };
  96. var _highlight = function (srcText, rstArray) {
  97. if (!srcText) {
  98. srcBackgroundElm.html('');
  99. return;
  100. }
  101. var hl = [];
  102. var preIndex = 0;
  103. $.each(rstArray, function (i, item) {
  104. if (i === 0) {
  105. if (item.index === 0) {
  106. hl.push(_createTag(TAG_MATCHED, item));
  107. } else {
  108. hl.push(_createTag(TAG_NOT_MATCHED, {
  109. index:0,
  110. text:srcText.substring(0, item.index)
  111. }));
  112. hl.push(_createTag(TAG_MATCHED, item));
  113. }
  114. } else {
  115. preIndex = rstArray[i - 1].index + rstArray[i - 1].text.length;
  116. hl.push(_createTag(TAG_NOT_MATCHED, {
  117. index:preIndex,
  118. text:srcText.substring(preIndex, item.index)
  119. }));
  120. hl.push(_createTag(TAG_MATCHED, item));
  121. }
  122. });
  123. srcBackgroundElm.html(hl.join(''));
  124. _blinkHighlight();
  125. };
  126. var _emptyTable = function (message) {
  127. var tbl = ["<table class='table table-bordered table-striped table-condensed table-hover'>"];
  128. tbl.push('<tr class="active"><th class="num">序号</th><th>匹配结果</th></tr>');
  129. tbl.push('<tr><td colspan="2">' + message + '</td></tr>');
  130. tbl.push('</table>');
  131. return tbl.join('');
  132. };
  133. var _dealRegMatch = function (e) {
  134. srcWrapperElm.height(srcElm.height() + 24);
  135. var regTxt = regElm.val().trim();
  136. var srcTxt = srcElm.val().trim();
  137. if (!regTxt || !srcTxt) {
  138. rstElm.html(_emptyTable('不能匹配'));
  139. rstCount.html('0个');
  140. _highlight();
  141. } else {
  142. var reg = _getRegExp(regTxt);
  143. if (!reg || !reg instanceof RegExp) {
  144. rstElm.html(_emptyTable('正则表达式错误!'));
  145. rstCount.html('0个');
  146. _highlight();
  147. return;
  148. }
  149. var rst = [];
  150. // 用字符串的replace方法来找到匹配目标在元字符串中的准确位置
  151. srcTxt.replace(reg, function () {
  152. var matchedTxt = arguments[0];
  153. var txtIndex = arguments[arguments.length - 2];
  154. rst.push({
  155. text:matchedTxt,
  156. index:txtIndex
  157. });
  158. });
  159. if (!rst || !rst.length) {
  160. rstElm.html(_emptyTable('不能匹配'));
  161. rstCount.html('0个');
  162. _highlight();
  163. } else {
  164. rstElm.html(_buildTable(rst));
  165. rstCount.html(rst.length + '个');
  166. _highlight(srcElm.val(), rst);
  167. }
  168. }
  169. };
  170. var _init = function () {
  171. $(function () {
  172. regElm = $('#regText');
  173. srcElm = $('#srcCode');
  174. srcBackgroundElm = $('#srcBackground');
  175. srcWrapperElm = $('#srcWrapper');
  176. rstElm = $('#rstCode').html(_emptyTable('暂无输入'));
  177. rstCount = $('#rstCount');
  178. regListElm = $('#regList');
  179. // 输入框自适应高度
  180. regElm.textareaAutoHeight({minHeight:34});
  181. srcElm.textareaAutoHeight({minHeight:50});
  182. srcBackgroundElm.textareaAutoHeight({minHeight:50});
  183. // 监听两个输入框的按键、paste、change事件
  184. $('#regText,#srcCode').keyup(_dealRegMatch).change(_dealRegMatch)
  185. .bind('paste', _dealRegMatch);
  186. regListElm.change(function (e) {
  187. var reg = $(this).val();
  188. var regTipElm = $('#regTip');
  189. regElm.val(reg);
  190. if (!reg) {
  191. regTipElm.hide();
  192. } else {
  193. regTipElm.show();
  194. }
  195. });
  196. });
  197. };
  198. return {
  199. init:_init
  200. };
  201. })();
  202. RegExpTools.init();