老师,关于代码优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < meta http-equiv = "X-UA-Compatible" content = "ie=edge" > < title >正则表达式测试工具</ title > < style > #RegExp{ width: 650px; margin: 100px auto; font-size: 14px; } #RegExp .title{ color: #777; font-size: 24px; text-align: center; } #RegExp .textbox{ width: 638px; height: 150px; border: 1px solid #ccc; border-radius: 5px; padding: 5px; resize: none; } #RegExp .readOnly{ background: #eee; } #RegExp .textfield{ width: 215px; padding: 5px; border: 1px solid #ccc; } </ style > </ head > < body > < div id = "RegExp" > < h1 class = "title" >正则表达式测试工具</ h1 > < textarea id = "userText" class = "textbox" placeholder = "在此输入带匹配的文本" ></ textarea > < p > 正则表达式:< input type = "text" id = "userRegExp" class = "textfield" placeholder = "在此输入正则表达式" > < input type = "checkbox" name = "userModifier" value = "i" >忽略大小写 < input type = "checkbox" name = "userModifier" value = "g" >全局匹配 < input type = "checkbox" name = "userModifier" value = "m" >多行匹配 < input type = "button" value = "测试匹配" id = "matchingBtn" > </ p > 匹配结果: < div id = "matchingResult" class = "textbox readOnly" ></ div > < p > 替换文本:< input type = "text" id = "userReplaceText" class = "textfield" placeholder = "在此输入替换文本" > < input type = "button" value = "替换" id = "replaceBtn" > </ p > 替换结果: < div id = "replaceResult" class = "textbox readOnly" ></ div > </ div > < script > var userText = byId("userText"), userRegExp = byId("userRegExp"), userModifier = document.getElementsByName("userModifier"), matchingBtn = byId("matchingBtn"), matchingResult = byId("matchingResult"), userReplaceText = byId("userReplaceText"), replaceBtn = byId("replaceBtn"), replaceResult = byId("replaceResult"); var pattern, modifier = ''; //封装getElementById function byId(id){ return typeof(id)==="string"?document.getElementById(id):id; } //模式修饰符igm的判断 for(var i = 0; i < userModifier.length; i++){ userModifier[i].onclick = function () { modifier = ''; for(var j = 0; j < userModifier.length; j++){ if(userModifier[j].checked){ modifier += userModifier[j].value; } } } } //检测是否输入 function checkInput(inputText){ if(!inputText.value){ if(inputText==userText){ alert("请输入待匹配的文本!"); }else if(inputText==userRegExp){ alert("请输入正则表达式!"); }else if(inputText==userReplaceText){ alert("请输入替换文本!"); } inputText.focus(); return; } } //结果输出 function resultInput(inputText){ pattern = new RegExp('('+userRegExp.value+')',modifier); inputText.innerHTML = pattern.exec(userText.value) ?userText.value.replace(pattern,"< span style = 'background-color:yellow;' >$1</ span >"):'(没有匹配)'; } //给测试匹配按钮绑定点击事件 matchingBtn.onclick = function () { //检测是否输入了待匹配文本 checkInput(userText); //检测是否输入了正则表达式 checkInput(userRegExp); resultInput(matchingResult); // matchingResult.innerHTML = pattern.exec(userText.value) ||'(没有匹配)'; //短路操作,如果匹配得到,返回匹配的内容;如果未匹配得到,返回(没有匹配) } //给替换按钮绑定点击事件 replaceBtn.onclick = function () { checkInput(userText); checkInput(userRegExp); //检测是否输入了替换文本 checkInput(userReplaceText); pattern = new RegExp('('+userRegExp.value+')',modifier); replaceResult.innerHTML = userText.value.replace(pattern,"< span style = 'color:red;' >"+ userReplaceText.value +"</ span >"); } </ script > </ body > </ html > |
我对getElementById进行了封装,还将检测是否输入相关文本的重复字段进行了提取,
在对结果输出部分进行函数封装时遇到了小困难,可以怎么整合代码?
除了我提到的这个问题之外,还有什么部分还可以优化吗
14
收起
正在回答 回答被采纳积分+1
2回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧