搜索文章:

首页  |  Java技术  |  Asp.net  |  Asp编程  |  VC/C++  |  Delphi  |  VB编程
上一篇:使用组件搜索 >>

在ASP中使用类

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)
%>

<br>
<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

()

相关文章:
© 2006   www.java-asp.net