asp正則表示式使用過程中踩的坑
以下程式碼是我除錯過程中的程式碼,有以下問題:
1、matches(0).SubMatches(0)是匹配模式中有()的時候來獲取結果。
2、如果匹配模式中無()則直接用Match.value獲取匹配結果
3、將以下程式碼copy到test.asp的檔案中時,在除錯的時候瀏覽器有些時候看不到結果,是因為輸出的結果中包涵了程式碼。採用原始碼的模式是可以看到匹配的程式碼及文字。Match.value輸出的結果是包涵了程式碼的。matches(0).SubMatches(0)輸出的結果是可能剔除了程式碼的結果。
4、未解決的問題:html中的空格及換行,如stingtxt2的樣式,沒有找到剔除空格或換行的解決辦法。歡迎交流:QQ:309800818
<% Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' 建立變數。 Set regEx = New RegExp ' 建立正則表示式。 regEx.Pattern = patrn ' 設定模式。 regEx.IgnoreCase = True ' 設定是否區分大小寫。 regEx.Global = True ' 設定全程可用性。 Set Matches = regEx.Execute(strng) ' 執行搜尋。 For Each Match in Matches ' 遍歷 Matches 集合。 RetStr = matches(0).SubMatches(0) 'RetStr = RetStr & Match.value &"<br/>" 'RetStr = RetStr & Matches(1) &"<br/>" Next RegExpTest = RetStr End Function
stingtxt = "<title>fasfasdfasdfadsfasdfdfasd</title>" stingtxt2 = "<title> 惜命最好的方式,竟不是養生,而是…驚醒無數人! </title>" 'response.Write RegExpTest("<img.*?>","asdfasdfadsf<img src='ad.jpg'>asdfasdfasdfsdf<img src='bbbd.jpg'>") response.Write(RegExpTest("<title>(.*?)<\/title>", stingtxt2)) 'response.Write(RegExpTest("(<title>)", "<title>fasdfasd</title>")) 'response.write RegExpTest("[ij]s.", "IS1 Js2 IS3 is4") %>