学院首页>网络编程>ASP>在ASP中使用类

在ASP中使用类

作者:未知 来源:未知 添加时间:2006-5-21 10:27:51
VBScript5中增加了许多新功能,最振奋人心的当属类和正则表达式的出现。以下是本人写的一个解析html代码的类。我是学php的,语法有不习惯的地方,请大家多包含。



<%

Class HTMLParse



' 设置 Initialize 事件。

Private Sub Class_Initialize

myGlobal = True

myIgnoreCase = True

End Sub



Property Let Global(g)

Dim regEx ' 建立变量。

Set regEx = New RegExp ' 建立正则表达式。

regEx.Pattern = "True/False/1/0" ' 设置模式。

regEx.IgnoreCase = True ' 设置是否区分大小写。

If regEx.Test(CStr(g)) Then 

myGlobal = g

Else 

Call Halt("无效Global参数配置")

End If 

End Property



Property Get Global()

Global = myGlobal

End Property



Property Let IgnoreCase(c)

Dim regEx 

Set regEx = New RegExp

regEx.Pattern = "True/False/1/0"

regEx.IgnoreCase = True 

If regEx.Test(CStr(c)) Then 

myIgnoreCase = c

Else 

Call Halt("无效IgnoreCase参数配置")

End If

End Property



Property Get IgnoreCase()

IgnoreCase = myIgnoreCase

End Property



'解析所有HTML标记的函数

Public Function Parse(input)

Parse = "<table border=1 width=50% align=center>" & vbCrLf

Dim regEx , regVal , match , i



Set regEx = New RegExp

regEx.Pattern = "<([a-z]\w*)(?:.*?)>(.*)<\/\1>"

regEx.Global = myGlobal

regEx.IgnoreCase = myIgnoreCase



Set regVal = regEx.Execute(Trim(input))

If regVal.Count > 0 Then '如果发现匹配元素

Parse = Parse & "<caption>发现" & regVal.Count & "个HTML标记</caption>" & vbCrLf

Parse = Parse & "<tr align=center><th>编号</th><th>匹配标记<th>匹配显示</th></tr>" & vbCrLf

For i=0 To regVal.Count-1

Set match = regVal(i)

Parse = Parse & "<tr align=center>" & vbCrLf

Parse = Parse & "<td>" & i+1 & "</td><td>" & match.SubMatches(0) & "</td><td>" & match & "</td>" & vbCrLf

Parse = Parse & "</tr>" & vbCrLf

Next

Else Parse = Parse & "<caption>没有发现HTML标记</caption>" & vbCrLf

End If

Parse = Parse & "</table>" & vbCrLf

End Function



'解析指定HTML标记的函数

Public Function ParseTag(input,tag)

ParseTag = "<table border=1 width=50% align=center>" & vbCrLf

Dim regEx , regVal , match , i



Set regEx = New RegExp

regEx.Pattern = "<(" & tag & ")(?:.*?)>(.*?)<\/\1>"

regEx.Global = myGlobal

regEx.IgnoreCase = myIgnoreCase



Set regVal = regEx.Execute(Trim(input))

If regVal.Count > 0 Then '如果发现匹配元素

ParseTag = ParseTag & "<caption>发现" & regVal.Count & "个" & UCase(tag) & "标记</caption>" & vbCrLf

ParseTag = ParseTag & "<tr align=center><th>编号</th><th>发现位置<th>包含内容</th></tr>" & vbCrLf

For i=0 To regVal.Count-1

Set match = regVal(i)

ParseTag = ParseTag & "<tr align=center>" & vbCrLf

ParseTag = ParseTag & "<td>" & i+1 & "</td><td>" & match.FirstIndex & "</td><td>" & match.SubMatches(1) & "</td>" & vbCrLf

ParseTag = ParseTag & "</tr>" & vbCrLf

Next

Else ParseTag = ParseTag & "<caption>没有发现" & UCase(tag) & "标记</caption>" & vbCrLf

End If

ParseTag = ParseTag & "</table>" & vbCrLf

End Function



'打印错误

Private Sub Halt(errstr)

Response.Write("<font color=red size=3>" & errstr & "</font>" & vbCrLf)

Call Class_Terminate

End Sub



Private Sub Class_Terminate ' 设置 Terminate 事件。

End Sub



'定义两个内部变量

Private myGlobal

Private myIgnoreCase



End Class

%>





<html>

<body>

<div align=center><h2>范例1</h2></div>

<%

'范例1

Dim input , result

input = "<i>这是</i>一个<font color=green>VBScript</font>的<b>正则<i>表达式</i>范例</b>。"



Set hp = New HTMLParse

hp.Global = 1

hp.IgnoreCase = False

result = hp.Parse(input)

Response.Write(result)

%>






<div align=center><h2>范例2</h2></div>

<%

'范例2

'hp.Global = 1

'hp.IgnoreCase = False

result2 = hp.ParseTag(input,"i")

Response.Write(result2)

Set hp = Nothing

%>



</body>

</html>



大家应该注意到了,VBScript的正则表达式和Jscript的解析是一样的,只是语法不同。关于最新的VBScript的文档,大家可以到微软中国的脚本技术主页去下载,网址:http://www.microsoft.com/china/scripting
站内搜索