<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <br/><br/> <input type="text" id="regtxt"/> <br/> <button onclick="getreg()">操作正则表达式/^fo+$/</button> <button onclick="getreg1()">操作正则表达式/^fo*$/</button> <button onclick="getreg2()">操作正则表达式/^fo?$/</button> <div id="div1"></div> <script> // ^:一个字符串的开始 $:一个字符串的结束 function getreg(){ var reg=/^fo+$/; //限制字符串长度,这里无法获得输入框的数据 var div1 = document.getElementById('div1'); var regtxt = document.getElementById('regtxt'); div1.innerHTML=reg.test(regtxt.value); } function getreg1(){ var reg=/^fo*$/; var div1 = document.getElementById('div1'); var regtxt = document.getElementById('regtxt'); div1.innerHTML=reg.test(regtxt.value); } function getreg2(){ var reg=/^fo?$/; var div1 = document.getElementById('div1'); var regtxt = document.getElementById('regtxt'); div1.innerHTML=reg.test(regtxt.value); } </script> </body> </html>