VBScript5中增加了许多新功能,最振奋人心的当属类和正则表达式的出现。以下是本人写的一个解析html代码的类。我是学php的,语法有不习惯的地方,请大家多包含。
1
2Class HTMLParse
3
4' 设置 Initialize 事件。
5Private Sub Class_Initialize
6myGlobal = True
7myIgnoreCase = True
8End Sub
9
10Property Let Global(g)
11Dim regEx ' 建立变量。
12Set regEx = New RegExp ' 建立正则表达式。
13regEx.Pattern = "True|False|1|0" ' 设置模式。
14regEx.IgnoreCase = True ' 设置是否区分大小写。
15If regEx.Test(CStr(g)) Then
16myGlobal = g
17Else
18Call Halt("无效Global参数配置")
19End If
20End Property
21
22Property Get Global()
23Global = myGlobal
24End Property
25
26Property Let IgnoreCase(c)
27Dim regEx
28Set regEx = New RegExp
29regEx.Pattern = "True|False|1|0"
30regEx.IgnoreCase = True
31If regEx.Test(CStr(c)) Then
32myIgnoreCase = c
33Else
34Call Halt("无效IgnoreCase参数配置")
35End If
36End Property
37
38Property Get IgnoreCase()
39IgnoreCase = myIgnoreCase
40End Property
41
42'解析所有HTML标记的函数
43Public Function Parse(input)
44Parse = "
<table align="center" border="1" width="50%">" & 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></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>
1" & vbCrLf
2End Function
3
4'解析指定HTML标记的函数
5Public Function ParseTag(input,tag)
6ParseTag = "
<table align="center" border="1" width="50%">" & 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></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>
1" & vbCrLf
2End Function
3
4'打印错误
5Private Sub Halt(errstr)
6Response.Write("
<font color="red" size="3">" & errstr & "</font>
1" & vbCrLf)
2Call Class_Terminate
3End Sub
4
5Private Sub Class_Terminate ' 设置 Terminate 事件。
6End Sub
7
8'定义两个内部变量
9Private myGlobal
10Private myIgnoreCase
11
12End Class
1<html>
2<body>
3<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)
1
2<br/>
3<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
1
2</body>
3</html>
大家应该注意到了,VBScript的正则表达式和Jscript的解析是一样的,只是语法不同。关于最新的VBScript的文档,大家可以到微软中国的脚本技术主页去下载,网址:http://www.microsoft.com/china/scripting