在ASP中使用类 (1)
VBScript5中增加了许多新功能,最振奋人心的当属类和正则表达式的出现。以下是本人写的一个解析html代码的类。我是
学php的,语法有不习惯的地方,请大家多包含。
%26lt;%
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 = %26quot;True|False|1|0%26quot;' 设置模式。
regEx.IgnoreCase = True' 设置是否区分大小写。
If regEx.Test(CStr(g)) Then
myGlobal = g
Else
Call Halt(%26quot;无效Global参数配置%26quot;)
End If
End Property
Property Get Global()
Global = myGlobal
End Property
Property Let IgnoreCase(c)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = %26quot;True|False|1|0%26quot;
regEx.IgnoreCase = True
If regEx.Test(CStr(c)) Then
myIgnoreCase = c
Else
Call Halt(%26quot;无效IgnoreCase参数配置%26quot;)
End If
End Property
Property Get IgnoreCase()
IgnoreCase = myIgnoreCase
End Property
'解析所有HTML标记的函数
Public Function Parse(input)
Parse = %26quot;%26lt;table border=1 width=50% align=center%26gt;%26quot; %26amp; vbCrLf
Dim regEx , regVal , match , i
Set regEx = New RegExp
regEx.Pattern = %26quot;%26lt;([a-z]\w*)(?:.*?)%26gt;(.*)%26lt;\/\1%26gt;%26quot;
regEx.Global = myGlobal
regEx.IgnoreCase = myIgnoreCase
Set regVal = regEx.Execute(Trim(input))
If regVal.Count %26gt; 0 Then '假如发现匹配元素
Parse = Parse %26amp; %26quot;%26lt;caption%26gt;发现%26quot; %26amp; regVal.Count %26amp; %26quot;个HTML标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;tr align=center%26gt;%26lt;th%26gt;编号%26lt;/th%26gt;%26lt;th%26gt;匹配标记%26lt;th%26gt;匹配显示%26lt;/th%26gt;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
For i=0 To regVal.Count-1
Set match = regVal(i)
Parse = Parse %26amp; %26quot;%26lt;tr align=center%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;td%26gt;%26quot; %26amp; i+1 %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match.SubMatches(0) %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match
%26amp; %26quot;%26lt;/td%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
Next
Else Parse = Parse %26amp; %26quot;%26lt;caption%26gt;没有发现HTML标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
End If
Parse = Parse %26amp; %26quot;%26lt;/table%26gt;%26quot; %26amp; vbCrLf
End Function
'解析指定HTML标记的函数
Public Function ParseTag(input,tag)
ParseTag = %26quot;%26lt;table border=1 width=50% align=center%26gt;%26quot; %26amp; vbCrLf
Dim regEx , regVal , match , i
Set regEx = New RegExp
regEx.Pattern = %26quot;%26lt;(%26quot; %26amp; tag %26amp; %26quot;)(?:.*?)%26gt;(.*?)%26lt;\/\1%26gt;%26quot;
regEx.Global = myGlobal
regEx.IgnoreCase = myIgnoreCase
Set regVal = regEx.Execute(Trim(input))
If regVal.Count %26gt; 0 Then '假如发现匹配元素
ParseTag = ParseTag %26amp; %26quot;%26lt;caption%26gt;发现%26quot; %26amp; regVal.Count %26amp; %26quot;个%26quot; %26amp; UCase(tag) %26amp; %26quot;标记%26lt;/caption%26gt;%26quot; %26amp;
vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;tr align=center%26gt;%26lt;th%26gt;编号%26lt;/th%26gt;%26lt;th%26gt;发现位置%26lt;th%26gt;包含内容%26lt;/th%26gt;%26lt;/tr%26gt;%26quot; %26amp;
vbCrLf
For i=0 To regVal.Count-1
Set match = regVal(i)
ParseTag = ParseTag %26amp; %26quot;%26lt;tr align=center%26gt;%26quot; %26amp; vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;td%26gt;%26quot; %26amp; i+1 %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match.FirstIndex %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp;
match.SubMatches(1) %26amp; %26quot;%26lt;/td%26gt;%26quot; %26amp; vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
Next
Else ParseTag = ParseTag %26amp; %26quot;%26lt;caption%26gt;没有发现%26quot; %26amp; UCase(tag) %26amp; %26quot;标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
End If
ParseTag = ParseTag %26amp; %26quot;%26lt;/table%26gt;%26quot; %26amp; vbCrLf
End Function
'打印错误
Private Sub Halt(errstr)
Response.Write(%26quot;%26lt;font color=red size=3%26gt;%26quot; %26amp; errstr %26amp; %26quot;%26lt;/font%26gt;%26quot; %26amp; vbCrLf)
Call Class_Terminate
End Sub
Private Sub Class_Terminate' 设置 Terminate 事件。
End Sub
'定义两个内部变量
Private myGlobal
Private myIgnoreCase
End Class
%%26gt;
%26lt;html%26gt;
%26lt;body%26gt;
%26lt;div align=center%26gt;%26lt;h2%26gt;范例1%26lt;/h2%26gt;%26lt;/div%26gt;
%26lt;%
'范例1
Dim input , result
input = %26quot;%26lt;i%26gt;这是%26lt;/i%26gt;一个%26lt;font color=green%26gt;VBScript%26lt;/font%26gt;的%26lt;b%26gt;正则%26lt;i%26gt;表达式%26lt;/i%26gt;范例%26lt;/b%26gt;。%26quot;
Set hp = New HTMLParse
hp.Global = 1
hp.IgnoreCase = False
result = hp.Parse(input)
Response.Write(result)
%%26gt;
%26lt;br%26gt;
%26lt;div align=center%26gt;%26lt;h2%26gt;范例2%26lt;/h2%26gt;%26lt;/div%26gt;
%26lt;%
'范例2
'hp.Global = 1
'hp.IgnoreCase = False
result2 = hp.ParseTag(input,%26quot;i%26quot;)
Response.Write(result2)
Set hp = Nothing
%%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
大家应该注重到了,VBScript的正则表达式和Jscript的解析是一样的,只是语法不同。关于最新的VBScript的文档,大家
可以到微软中国的脚本技术主页去下载,网址http://www.microsoft.com/china/scripting
学php的,语法有不习惯的地方,请大家多包含。
%26lt;%
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 = %26quot;True|False|1|0%26quot;' 设置模式。
regEx.IgnoreCase = True' 设置是否区分大小写。
If regEx.Test(CStr(g)) Then
myGlobal = g
Else
Call Halt(%26quot;无效Global参数配置%26quot;)
End If
End Property
Property Get Global()
Global = myGlobal
End Property
Property Let IgnoreCase(c)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = %26quot;True|False|1|0%26quot;
regEx.IgnoreCase = True
If regEx.Test(CStr(c)) Then
myIgnoreCase = c
Else
Call Halt(%26quot;无效IgnoreCase参数配置%26quot;)
End If
End Property
Property Get IgnoreCase()
IgnoreCase = myIgnoreCase
End Property
'解析所有HTML标记的函数
Public Function Parse(input)
Parse = %26quot;%26lt;table border=1 width=50% align=center%26gt;%26quot; %26amp; vbCrLf
Dim regEx , regVal , match , i
Set regEx = New RegExp
regEx.Pattern = %26quot;%26lt;([a-z]\w*)(?:.*?)%26gt;(.*)%26lt;\/\1%26gt;%26quot;
regEx.Global = myGlobal
regEx.IgnoreCase = myIgnoreCase
Set regVal = regEx.Execute(Trim(input))
If regVal.Count %26gt; 0 Then '假如发现匹配元素
Parse = Parse %26amp; %26quot;%26lt;caption%26gt;发现%26quot; %26amp; regVal.Count %26amp; %26quot;个HTML标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;tr align=center%26gt;%26lt;th%26gt;编号%26lt;/th%26gt;%26lt;th%26gt;匹配标记%26lt;th%26gt;匹配显示%26lt;/th%26gt;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
For i=0 To regVal.Count-1
Set match = regVal(i)
Parse = Parse %26amp; %26quot;%26lt;tr align=center%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;td%26gt;%26quot; %26amp; i+1 %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match.SubMatches(0) %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match
%26amp; %26quot;%26lt;/td%26gt;%26quot; %26amp; vbCrLf
Parse = Parse %26amp; %26quot;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
Next
Else Parse = Parse %26amp; %26quot;%26lt;caption%26gt;没有发现HTML标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
End If
Parse = Parse %26amp; %26quot;%26lt;/table%26gt;%26quot; %26amp; vbCrLf
End Function
'解析指定HTML标记的函数
Public Function ParseTag(input,tag)
ParseTag = %26quot;%26lt;table border=1 width=50% align=center%26gt;%26quot; %26amp; vbCrLf
Dim regEx , regVal , match , i
Set regEx = New RegExp
regEx.Pattern = %26quot;%26lt;(%26quot; %26amp; tag %26amp; %26quot;)(?:.*?)%26gt;(.*?)%26lt;\/\1%26gt;%26quot;
regEx.Global = myGlobal
regEx.IgnoreCase = myIgnoreCase
Set regVal = regEx.Execute(Trim(input))
If regVal.Count %26gt; 0 Then '假如发现匹配元素
ParseTag = ParseTag %26amp; %26quot;%26lt;caption%26gt;发现%26quot; %26amp; regVal.Count %26amp; %26quot;个%26quot; %26amp; UCase(tag) %26amp; %26quot;标记%26lt;/caption%26gt;%26quot; %26amp;
vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;tr align=center%26gt;%26lt;th%26gt;编号%26lt;/th%26gt;%26lt;th%26gt;发现位置%26lt;th%26gt;包含内容%26lt;/th%26gt;%26lt;/tr%26gt;%26quot; %26amp;
vbCrLf
For i=0 To regVal.Count-1
Set match = regVal(i)
ParseTag = ParseTag %26amp; %26quot;%26lt;tr align=center%26gt;%26quot; %26amp; vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;td%26gt;%26quot; %26amp; i+1 %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp; match.FirstIndex %26amp; %26quot;%26lt;/td%26gt;%26lt;td%26gt;%26quot; %26amp;
match.SubMatches(1) %26amp; %26quot;%26lt;/td%26gt;%26quot; %26amp; vbCrLf
ParseTag = ParseTag %26amp; %26quot;%26lt;/tr%26gt;%26quot; %26amp; vbCrLf
Next
Else ParseTag = ParseTag %26amp; %26quot;%26lt;caption%26gt;没有发现%26quot; %26amp; UCase(tag) %26amp; %26quot;标记%26lt;/caption%26gt;%26quot; %26amp; vbCrLf
End If
ParseTag = ParseTag %26amp; %26quot;%26lt;/table%26gt;%26quot; %26amp; vbCrLf
End Function
'打印错误
Private Sub Halt(errstr)
Response.Write(%26quot;%26lt;font color=red size=3%26gt;%26quot; %26amp; errstr %26amp; %26quot;%26lt;/font%26gt;%26quot; %26amp; vbCrLf)
Call Class_Terminate
End Sub
Private Sub Class_Terminate' 设置 Terminate 事件。
End Sub
'定义两个内部变量
Private myGlobal
Private myIgnoreCase
End Class
%%26gt;
%26lt;html%26gt;
%26lt;body%26gt;
%26lt;div align=center%26gt;%26lt;h2%26gt;范例1%26lt;/h2%26gt;%26lt;/div%26gt;
%26lt;%
'范例1
Dim input , result
input = %26quot;%26lt;i%26gt;这是%26lt;/i%26gt;一个%26lt;font color=green%26gt;VBScript%26lt;/font%26gt;的%26lt;b%26gt;正则%26lt;i%26gt;表达式%26lt;/i%26gt;范例%26lt;/b%26gt;。%26quot;
Set hp = New HTMLParse
hp.Global = 1
hp.IgnoreCase = False
result = hp.Parse(input)
Response.Write(result)
%%26gt;
%26lt;br%26gt;
%26lt;div align=center%26gt;%26lt;h2%26gt;范例2%26lt;/h2%26gt;%26lt;/div%26gt;
%26lt;%
'范例2
'hp.Global = 1
'hp.IgnoreCase = False
result2 = hp.ParseTag(input,%26quot;i%26quot;)
Response.Write(result2)
Set hp = Nothing
%%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
大家应该注重到了,VBScript的正则表达式和Jscript的解析是一样的,只是语法不同。关于最新的VBScript的文档,大家
可以到微软中国的脚本技术主页去下载,网址http://www.microsoft.com/china/scripting





