[ASP]在asp中使用RegExp物件

有關網頁及相關語法的討論
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

[ASP]在asp中使用RegExp物件

文章 tim »

在asp中使用RegExp物件

在 asp 中若要用 Regular Expression 的話, 就非 RegExp 物件不可了, 它的功能強大, 而且也十分方便, 下面的資料為相關的連結,

http://msdn.microsoft.com/library/defau ... 696d0a.asp
http://msdn.microsoft.com/library/defau ... d965f1.asp

以下舉個例子, 使用 RegExp 找出 html code 的方法:

代碼: 選擇全部

    
    sMessage = "haha <  font color=red size=2> tetest< /fOnt  > really? <ht>  223</ht >ddd" 
    Response.Write "ori:" & Server.HTMLEncode(sMessage) & "< BR >" 
    Response.Write "result:" & Server.HTMLEncode(ReplaceTagTest(sMessage)) 
     
     
    Function ReplaceTagTest(strMessage) 
      Set RegularExpressionObject = New RegExp 
     
      With RegularExpressionObject 
        .Pattern = "<[s]*([^<>/]+)[s]*[^>]*>([^<>]*)<[s]*/1[s]*>" 
        .IgnoreCase = True 
        .Global = True 
      End With 
     
      Set expressionmatch = RegularExpressionObject.Execute(strMessage) 
     
      If expressionmatch.Count > 0 Then 
        For Each expressionmatched in expressionmatch 
          strMessageModify = strMessageModify & expressionmatched.value & "--"       
        Next     
        ReplaceTagTest = strMessageModify 
      End If 
     
      Set RegularExpressionObject = Nothing 
    End Function 

執行結果如下,
ori:haha < font color=red size=2> tetest< /fOnt > really? 223ddd
result:< font color=red size=2> tetest< /fOnt >-- 223--

利用Pattern "<[s]*([^<>/]+)[s]*[^>]*>([^<>]*)<[s]*/1[s]*>" 可以找出html code, 而利用 1 可以將第一個 pattern match 的內容, 利用 1 在後面的 tag 再找出來, 如此便能方便地將 html 做拆解囉!
多多留言, 整理文章, 把經驗累積下來.....
回覆文章