(原创)正则表达式练习测试页

类别:Asp 点击:0 评论:0 推荐:

前段时间差不多都要和正则表达式打交道,所以当时就弄了这个测试练习页面.把以下代码保存为一个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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="IgnoreCase" type="checkbox" class="checkbox" value="1">
          <span title="不区分字母的大小写">不区分大小写</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input name="Global" type="checkbox" class="checkbox" value="1">         
          <span title="查找所有的可匹配项">全局模式查找</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <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, ">", "&gt;")
  fString = Replace(fString, "<", "&lt;")
  fString = Replace(fString, " ", "&nbsp;")
  fString = Replace(fString, CHR(32), "<I></I>&nbsp;")
  fString = Replace(fString, CHR(9), "&nbsp;")
  fString = Replace(fString, CHR(34), "&quot;")
  fString = Replace(fString, CHR(39), "&#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>

本文地址:http://com.8s8s.com/it/it7769.htm