老师帮忙看下哪里出错了
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 | <!DOCTYPE html> <html> <head> <meta charset= "UTF-8" /> <title>正则表达式测试工具</title> <style type= "text/css" > *{box-sizing: border-box;} #regExp{width: 650px; margin: 100px auto; font-size: 14px;} #regExp .title{color: #777; font-size: 24px; text-align: center;} #regExp .textBox{width: 650px; height: 150px; border: 1px solid #ccc; border-radius: 5px; padding: 5px; resize: none;} #regExp .textfield{width: 225px; padding: 5px; border: 1px solid #ccc;} .readOnly{background-color: #eee;} </style> </head> <body> <div id= "regExp" > <h1 class= "title" >正则表达式测试工具</h1> <textarea id= "userText" class= "textBox" rows= "10" cols= "30" placeholder= "在此输入待匹配的文本" ></textarea> <p> <span>正则表达式</span> <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" id= "matchBtn" value= "测试匹配" /> </p> <h4>匹配结果:</h4> <div id= "matchingResult" class= "textBox readOnly" rows= "10" cols= "30" ></div> <p> <span>替换文本</span> <input type= "text" id= "userReplaceText" class= "textfield" placeholder= "在此输入替换文本" /> <input type= "button" id= "replaceBtn" value= "替换文本" /> </p> <h4>匹配结果:</h4> <div id= "replaceResult" class= "textBox readOnly" rows= "10" cols= "30" ></div> </div> <script type= "text/javascript" > var userText = document.getElementById( "userText" ), matchBtn = document.getElementById( "matchBtn" ), userMod = document.getElementsByName( "userModifier" ), replaceBtn = document.getElementById( "replaceBtn" ), userRegExp = document.getElementById( "userRegExp" ), matchingResult = document.getElementById( "matchingResult" ), userReplaceText = document.getElementById( "userReplaceText" ), replaceResult = document.getElementById( "replaceResult" ), mod = "" , pattern; for ( var i = 0; i < userMod.length; i++) { console.log(userMod) userMod[i].onclick = function (){ mod= "" ; for ( var j = 0; j < userMod.length; j++) { if (userMod[j].ckecked) { mod+= userMod[j].value; } } } } matchBtn.onclick = function (){ pattern = new RegExp( "(" +userRegExp.value+ ")" , mod); matchingResult.innerHTML = pattern.exec(userText.value)?userText.value.replace(pattern, '<span style="background-color: yellow;">$1</span>' ):( "没有匹配" ) } // replaceBtn.onclick = function(){ // pattern = new RegExp("("+userRegExp.value+")", mod); // matchingResult.innerHTML = pattern.exec(userText.value)?userText.value.replace(pattern, '<span style="background-color: yellow;">$1</span>'):("没有匹配") // } </script> </body> </html> |
修饰符点了没用,哪里出了问题???? 只会匹配第一个单词,自己找了好久没找出来
2
收起
正在回答
3回答
同学你好!
抱歉昨天老师这边测得不是很全面。今天仔细看了一下问题应该是这里:checke单词拼写错误。
如果帮助到了你 欢迎采纳 祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧