igm只能写在构造表达式里么?test,exec后面不可以么?
来源:2-14 正则表达式测试工具--JS
顾兆昱
2019-10-19 10:49:30
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>正则表达式</title> <style type="text/css"> .container { width: 800px; height: 700px; background-color: pink; margin: 100px auto; text-align: center } textarea { height: 200px; width: 550px; resize: none } input:last-child { font-size: 40px; width: 70px } </style> </head> <body> <div class="container"> <h1>正则表达式</h1> <textarea name="input" rows="8" cols="80" id="inputarea"></textarea> <p>正则表达式:<input type="text" name="" value="" id="expinput"> <input type="checkbox" name="usermodify" value="i" id="ibtn">忽略大小写 <input type="checkbox" name="usermodify" value="g" id="gbtn">全局匹配 <input type="checkbox" name="usermodify" value="m" id="mbtn">多行匹配 <input type="button" name="" value="测试匹配" id="testbtn"> </p> <textarea name="result" rows="8" cols="80" disabled id="resultarea"></textarea> </div> <script type="text/javascript"> // var str = 'regular expression = regexp'; // /*此处写代码*/ // var exp = /lar exp/; // console.log(exp.test(str)); // console.log(exp.exec(str)) var textarea = document.getElementById("textarea"), expinput = document.getElementById("expinput"), ibtn = document.getElementById("ibtn"), gbtn = document.getElementById("gbtn"), mbtn = document.getElementById("mbtn"), testbtn = document.getElementById("testbtn"), resultarea = document.getElementById("resultarea"); usermodify = document.getElementsByName("usermodify"); //igm选项判断 function setModifier() { var modifier = ""; for (var i = 0; i < usermodify.length; i++) { if (usermodify[i].checked == true) { modifier += usermodify[i].value } } return modifier; } //设置正则表达式 function setExp() { var setExptext = new RegExp(expinput.value, setModifier()); return setExptext; } //正则匹配,返回匹配结果 function match() { var checkResult = setExp().exec(inputarea.value); return checkResult; } //设置输出结果 function setResult() { resultarea.innerHTML = match(); return; } //绑定点击事件 testbtn.addEventListener("click", function() { setResult(); }) </script> </body> </html>
1回答
一路电光带火花
2019-10-19
对鸭,i,g,m是正则表达式里的修饰符,不能用在test方法里
相似问题
回答 2
回答 3