js正则笔记
1 //内容 2 var innerhtml = $('.reading_box_m').html().replace(/ tag="?[一二三四五六七八九十]+"?/ig, ""); 3 var innerhtml = innerhtml.replace(/ id="?\d+"?/ig, ""); 4 //初步匹配篇 章 节 5 var regALl_Rge1 = new RegExp('<(div|p)\\s+class="?(ue_codeTitle|ue_chapterTitle|ue_sectionTitle)"?>([^<]+)</\\1>', 'ig'); 6 var titleLis = innerhtml.match(regALl_Rge1); 7 8 9 innerhtml.replace(regALl_Rge1, function () { 10 lip = lip + 1; 11 //篇 12 var format_1_children = $('<li><a href="#"></a></li>'); 13 //章 14 var format_2_children = $('<li><a href="#"></a></li>'); 15 //节 16 var format_3_children = $('<li><a href="#"></a></li>'); 17 var args = arguments; 18 //篇 19 if (args[2] == 'ue_codeTitle') { 20 format_1_children.children('a').prepend('<input type="button" >').attr('href', '#' + lip).append(args[3]); 21 format_1.append(format_1_children); 22 //当前篇 23 currF1 = format_1.children('li').last(); 24 f = f + 1; 25 } 26 //章 27 else if (args[2] == 'ue_chapterTitle') { 28 if (f == 0 || f != lastF) { 29 format_2_children.children('a').prepend('<input type="button" >').attr('href', '#' + lip).append(args[3]); 30 //当前章 31 currF2 = $("<ul id='general' class='manual_book' style='display: none;'></ul>"); 32 currF2.append(format_2_children); 33 //如果没有篇 34 if (!currF1) { 35 format_1.append(format_1_children); 36 //当前篇 37 currF1 = format_1.children('li').last(); 38 currF1.children('a').after(currF2).remove(); 39 currF2.show(); 40 f = 1; 41 } 42 else { 43 currF1.children('a').after(currF2); 44 } 45 } 46 else { 47 format_2_children.children('a').prepend('<input type="button" >').attr('href', '#' + lip).append(args[3]); 48 currF1.children('ul').append(format_2_children); 49 } 50 f2 = f2 + 1; 51 lastF = f; 52 } 53 //节 54 else if (args[2] == 'ue_sectionTitle') { 55 format_3_children.children('a').prepend('<input type="button" >').attr('href', '#' + lip).append(args[3]); 56 if (f2 == 0 || f2 != lastF2) { 57 //当前节 58 currF3 = $("<ul id='manual_1' class='list' style='display: none;' style='display: block;'></ul>"); 59 currF2.children('li').last().children('a').end().append(currF3); 60 } 61 currF3.append(format_3_children); 62 lastF2 = f2; 63 } 64 }); 65 66 67 68 69 /*********收索关键字变色开始***********/ 70 function HightLight(searchList) { 71 var arrStyle = [ 72 { style: "color:red;padding:0px 2px;" }, 73 { style: "color:orange;padding:0px 2px;" }, 74 { style: "color:yellow;padding:0px 2px;" }, 75 { style: "color:green;padding:0px 2px;" }, 76 { style: "color:aqua;padding:0px 2px;" }, 77 { style: "color:blue;padding:0px 2px;" }, 78 { style: "color:purple;padding:0px 2px;" } 79 ]; 80 searchList = decodeURIComponent(searchList); 81 if (!searchList) return; 82 for (var j = 0; j < searchList.split(',').length ; j++) { 83 var kyesVal = searchList.split(',')[j]; 84 $(".reading_box_m p").filter(function (i) { 85 if ($(this).text().indexOf(kyesVal) != -1) { 86 var r = RegExp(kyesVal, 'ig'); 87 $(this).html($(this).html().replace(r, '<span keys style=' + arrStyle[j % 7].style + ' >' + kyesVal + '</span>')); 88 } 89 }); 90 } 91 92 } 93 /*********收索关键字变色结束***********/ 94 95 96 //关键字搜索 97 function Sercah() { 98 var kyesVal = $.trim(document.getElementById('txtMenKey').value); 99 //if (keys_test != "" && keys_test == kyesVal) { return; }//waitmsg("已找到对应的" + g_keys_count + "处关键字!", 1);相同关键字查询时返回 100 $(".reading").children('reading_box_m p').css({ 'background': "#FFF" }) 101 $('span[keys]').removeAttr('style').removeAttr("id"); 102 g_keys = 0; 103 var lip = 0; 104 if (kyesVal != '' && kyesVal != '关键字查询') { 105 $(".reading_box_m p").filter(function (i) { 106 if ($(this).text().indexOf(kyesVal) != -1) { 107 var r = RegExp(kyesVal, 'ig'); 108 $(this).html($(this).text().replace(r, function () { 109 var arg = arguments; 110 lip++; 111 return '<span title=第' + lip + '处 id=keys' + lip + ' keys style="background:yellow; color:red">' + kyesVal + '</span>'; 112 })) 113 } 114 }); 115 keys_test = kyesVal; //关键字记录赋值 116 g_keys_count = lip; //关键字次数 117 if (arguments.length > 0 && arguments[0] == '0') {//有参数表示按钮点击没有查询到提示 118 if (lip == 0) 119 waitmsg("没有找到对应的关键字!", 4); 120 else { 121 waitmsg("已找到对应的" + g_keys_count + "处关键字!", 1); 122 PositionKeys('down', 0);//有参数是跳转到第一个查询位置 123 } 124 } 125 } else { 126 if (arguments.length > 0 && arguments[0] == '0')//有参数表示按钮点击没有查询到提示 127 $('#txtMenKey').val('关键字查询'); 128 keys_test = ''; 129 waitmsg("请输入查询关键字!", 4); 130 }; 131 }; 132 133 //定位关键字 134 function PositionKeys(t, c) { 135 136 137 var countkey = parseInt(g_keys_count); 138 if (countkey == 0) { 139 waitmsg("没有找到对应的关键字!", 4); 140 return; 141 } 142 if (t == "down") { 143 g_keys = g_keys + 1; 144 if (g_keys > countkey) { 145 g_keys = 1; 146 } 147 } 148 else if (t == "up") { 149 g_keys = g_keys - 1; 150 if (g_keys <= 0) { 151 g_keys = countkey; 152 } 153 } 154 $("span[id^='keys']").css("border", "none"); 155 $("#keys" + g_keys).css("border", "red 1px solid"); 156 var topVal = $('.reading_box').scrollTop() + parseInt($("#keys" + g_keys).offset().top); 157 $('.reading_box').stop(true).animate({ scrollTop: (topVal - 30) }, 500); 158 }; 159 160