一、正則表達式javascript
二、元字符css
三、出現次數html
四、正則表達式RegExp對象java
樣例:正則表達式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>表單驗證</title> <style type="text/css"> .right{ text-align:right;} .error{color:red;} </style> <script type="text/javascript"> function $ (id) { return document.getElementById(id); } function checkEmail () { var email = $("email").value; if (email == "") { document.getElementById("emailMsg").innerHTML = "Email不能爲空"; return false; } var reg = /^\w+@\w+\.\w{2,3}(\.\w{2,3})?$/; if (reg.test(email) == false) { document.getElementById("emailMsg").innerHTML = "Email格式不正確。"; return false; } return true; } function checkPwd () { var pwd = document.getElementById("pwd").value; if (pwd == ""){ $("pwdMsg").innerHTML = "密碼不能爲空。"; return false; } var reg = /^\w{6,}$/; if (reg.test(pwd)== false) { $("pwdMsg").innerHTML = "密碼長度不能小於6位。"; return false; } return true; } function checkRepwd () { var repwd = document.getElementById("repwd").value; if (repwd != pwd) { $("repwdMsg").innerHTML = "兩次輸入密碼不一致。"; return false; } return true; } function checkUser () { var user = document.getElementById("user").value; if (user == "") { $("userMsg").innerHTML = "姓名不能爲空。"; return false; } for (var i = 0 ; i < user.length ; i++) { var j = user.substring(i,i+1); if (isNaN(j) == false) { $("userMsg").innerHTML = "姓名中不能包含數字。"; return false; } } return true; } function check () { if (checkEmail() && checkPwd() && checkRepwd() && checkUser()) { return true; } else { return false; } } </script> </head> <body> <table id="center" border="0" cellpadding="0" cellspacing="3"> <tr> <td colspan="2" style="padding-left:120px; font-weight:bold;">註冊休閒網</td> </tr> <form action="" method="post" onsubmit="return check();" id="myForm"> <tr> <td class="right">您的email:</td> <td><input id="email" type="text" onblur="checkEmail();"/><span class="error" id="emailMsg"></span></td> </tr> <tr> <td class="right">輸入密碼:</td> <td><input id="pwd" type="password" onblur="checkPwd();"/><span class="error" id="pwdMsg"></span></td> </tr> <tr> <td class="right">再輸入一遍密碼:</td> <td><input id="repwd" type="password" onblur="checkRepwd();"/><span class="error" id="repwdMsg"></span></td> </tr> <tr> <td class="right">您的姓名:</td> <td><input id="user" type="text" onblur="checkUser();"/><span class="error" id="userMsg"></span></td> </tr> <tr> <td colspan="2" style="position:relative; left:130px;"><input type="submit" value="提交"/></td> </tr> </form> </table> </body> </html>
五、String對象支持正則表達式的方法post
樣例:ui
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>查找和替換</title> <script type="text/javascript"> function $ (id) { return document.getElementById(id); } function searchString () { var wordText = $("wordText").value; var findText = $("findText").value; var reg = eval("/" + findText + "/igm"); var resultArray = wordText.match(reg); alert("查找結果:" + resultArray); } function replaceString () { var wordText = $("wordText").value; var findText = $("findText").value; var reg = eval("/" + findText + "/igm"); var replaceText = $("replaceText").value; var resultArray = wordText.replace(reg,replaceText); alert("替換結果:" + resultArray); } </script> </head> <body> <table id="myForm" border="1" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"> <textarea id="wordText" cols="50" rows="10"> 黃金印痕! 破浪! 數頭黑色巨狼撲過來,陳默身形一動,迎上去,高級黃金印痕狀態下的破浪技能,一會兒掃中這數頭黑色巨狼。 數頭黑色巨狼的身形,再次一凝。 但單純破浪技能配合黃金印痕,沒法限制黑色巨狼十秒時間。 陳默再手一動,地裂刺技能用出,一道道地裂刺,出如今黑色巨狼身邊。 山崩地裂拳!</textarea> </td> </tr> <tr> <td><input type="button" onclick="searchString();" value="查找字符串"/></td> <td><input id="findText" type="text"/></td> </tr> <tr> <td><input type="button" onclick="replaceString();" value="替換字符串"/></td> <td><input id="replaceText" type="text"/></td> </tr> </table> </body> </html>
結果:spa