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

前段时间差不多都要和正则表达式打交道,所以当时就弄了这个测试练习页面.把以下代码保存为一个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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值