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 = "
| 编号 | 匹配标记 | 匹配显示 |
|---|---|---|
| " & i+1 & " | " & match.SubMatches(0) & " | " & match
& " | " & vbCrLf
End Function
'解析指定HTML标记的函数
Public Function ParseTag(input,tag)
ParseTag = "
| 编号 | 发现位置 | 包含内容 |
|---|---|---|
| " & i+1 & " | " & match.FirstIndex & " | " &
match.SubMatches(1) & " | " & vbCrLf
End Function
'打印错误
Private Sub Halt(errstr)
Response.Write("" & errstr & "" & vbCrLf)
Call Class_Terminate
End Sub
Private Sub Class_Terminate ' 设置 Terminate 事件。
End Sub
'定义两个内部变量
Private myGlobal
Private myIgnoreCase
End Class
%>
范例1
<%
'范例1
Dim input , result
input = "这是一个VBScript的正则表达式范例。"
Set hp = New HTMLParse
hp.Global = 1
hp.IgnoreCase = False
result = hp.Parse(input)
Response.Write(result)
%>
范例2
<%
'范例2
'hp.Global = 1
'hp.IgnoreCase = False
result2 = hp.ParseTag(input,"i")
Response.Write(result2)
Set hp = Nothing
%>
大家应该注意到了,VBScript的正则表达式和Jscript的解析是一样的,只是语法不同。关于最新的VBScript的文档,大家
可以到微软中国的脚本技术主页去下载,网址:http://www.microsoft.com/china/scripting

