正則表達式練習測試頁 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 正則表達式練習測試頁 (http://www.webasp.net/article/16/15480.htm) |
| -- 作者:未知 -- 發佈日期: 2004-12-13 |
| 前段時間差不多都要和正則表達式打交道,所以當時就弄了這個測試練習頁面.把以下代碼保存為一個HTML文件用IE打開即可使用:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>正則表達式測試</title> <style type="text/css"> <!-- td { font-family: "細明體"; font-size: 12px; color: #666666; text-decoration: none; } input { font-family: "細明體"; font-size: 12px; color: #666666; text-decoration: none; border: 1px solid #000000; height: 18px; } textarea { font-family: "細明體"; border: 1px solid #333333; word-spacing:inherit } .table { font-family: "細明體"; font-size: 12px; border: 1px solid #000000; white-space: normal; table-layout: fixed; WORD-BREAK: break-all; WORD-WRAP: break-word; display: table; } .checkbox { border: 1px dotted #CCCCCC; } --> </style> </head> <body> <table width="100%" height="306" border="0" cellpadding="0" cellspacing="0"> <tr> <td valign="top"><div align="center"> <table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td><div align="center"> <textarea name="InputBox" cols="100" rows="20" id="InputBox"></textarea> </div></td> </tr> </table> <table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="1"></td> </tr> </table> <table width="722" border="0" cellpadding="0" cellspacing="1" class="table"> <tr> <td height="25" bgcolor="#F2F2F2"><div align="center">正則表達式: <input name="RegExpBox" type="text" id="RegExpBox" size="100"> </div></td> </tr> </table> <table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="1"></td> </tr> </table> <table width="722" border="0" cellpadding="0" cellspacing="0" class="table"> <tr> <td width="580" height="25" bgcolor="#F2F2F2"><div align="center"> <input name="SingleLine" type="checkbox" class="checkbox" value="1"> <span title="將更改(.)的意思,使它包含所有字符">單行模式查找</span> <input name="IgnoreCase" type="checkbox" class="checkbox" value="1"> <span title="不區分字母的大小寫">不區分大小寫</span> <input name="Global" type="checkbox" class="checkbox" value="1"> <span title="查找所有的可匹配項">全局模式查找</span> <input name="ShowSub" type="checkbox" class="checkbox" value="1"> <span title="只顯示在()裡面的記錄集">只顯示子記錄集結果</span> </div></td> <td width="140" bgcolor="#F2F2F2"><input name="btnSearch" type="button" id="btnSearch" value="搜 索" onClick="vbscript:SearchRegExp()"></td> </tr> </table> <table width="700" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="1"></td> </tr> </table> <table width="722" border="0" cellpadding="0" cellspacing="1" class="table"> <tr> <td height="25" bgcolor="#FFFFCC"><div align="left" id="ShowText"> </div></td> </tr> </table> </div></td> </tr> </table> </body> <script language="vbscript"> Function SearchRegExp() Dim HTML,IgnoreCase,Global,Pattern,ShowSub HTML = document.all.InputBox.value Pattern = document.all.RegExpBox.value If Trim(HTML) = "" Or IsNull(HTML) Or Pattern = "" Then document.all.ShowText.innerHTML = "<font color='red'>---------請輸入要查找的文本---------</font>" Exit Function End If If document.all.SingleLine.checked Then HTML = Replace(HTML,VbCrlf,"") End If Global = document.all.Global.checked IgnoreCase = document.all.IgnoreCase.checked ShowSub = document.all.ShowSub.checked Dim RegExpObj,RegMatch,I,II,SubMatch Set RegExpObj = New RegExp RegExpObj.IgnoreCase = IgnoreCase RegExpObj.Global = Global RegExpObj.Pattern = Pattern Set RegMatch = RegExpObj.Execute(HTML) If RegMatch.Count < 1 Then document.all.ShowText.innerHTML = "<font color='red'>---------共查找到0個記錄---------</font>" Set RegMatch = Nothing Set RegExpObj = Nothing Exit Function End If document.all.ShowText.innerHTML = "<font color='red'>-----------------------查找開始--------------------</font><br>" Dim innerHTML innerHTML = document.all.ShowText.innerHTML For I = 0 To RegMatch.Count - 1 Set SubMatch = RegMatch(I).SubMatches If Not ShowSub Then innerHTML = innerHTML + "<font color='red'>記錄集" & I & ":</font><font color='blue'>" + HTMLEncode(RegMatch(I).Value) + "</font>" End If For II = 0 To SubMatch.Count - 1 innerHTML = innerHTML + "<br><font color='#FF00FF'>" +HTMLEncode(" 子記錄") & II & ":</font>" + HTMLEncode(SubMatch(II)) Next innerHTML = innerHTML + "<br><hr width=""100%"" size=""1"">" Next document.all.ShowText.innerHTML = innerHTML Set RegMatch = Nothing Set RegExpObj = Nothing End Function Function HTMLEncode(ByVal fString) If Not IsNull(fString) then fString = Replace(fString, ">", ">") fString = Replace(fString, "<", "<") fString = Replace(fString, " ", " ") fString = Replace(fString, CHR(32), "<I></I> ") fString = Replace(fString, CHR(9), " ") fString = Replace(fString, CHR(34), """) fString = Replace(fString, CHR(39), "'") fString = Replace(fString, CHR(13), "") fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ") fString = Replace(fString, CHR(10), "<BR> ") HTMLEncode = fString Else HTMLEncode="" End If End Function </script> </html> |
| webasp.net |