[ASP]RegExp對象提供簡單的正則表達式支持功能使用說明
2024-05-04 11:01:23
供稿:網友
RegExp對象的用法:
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 ' 遍歷匹配集合。
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match is '"
RetStr = RetStr & Match. & "'." & vbCRLF
Next
RegExpTest = RetStr
End
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
RegExp 對象的屬性
◎ Global屬性
Global屬性設置或返回一個 Boolean 值,該值指明在整個搜索字符串時模式是全部匹配還是只匹配第一個。
語法
object.Global [= True | False ]
object 參數總是 RegExp 對象。如果搜索應用于整個字符串,Global 屬性的值為 True,否則其值為 False。默認的設置為 True。
Global 屬性的用法(改變賦予 Global 屬性的值并觀察其效果):
RegExpTest(patrn, strng)
Dim regEx ' 建立變量。
Set regEx = New RegExp ' 建立規范表達式。
regEx.Pattern = patrn ' 設置模式。
regEx.IgnoreCase = True ' 設置是否區分字母的大小寫。
regEx.Global = True ' 設置全程性質。
RegExpTest = regEx.Execute(strng) ' 執行搜索。
End
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
◎ IgnoreCase屬性
IgnoreCase屬性設置或返回一個Boolean值,指明模式搜索是否區分大小寫。
語法
object.IgnoreCase [= True | False ]
object 參數總是一個 RegExp 對象。如果搜索是區分大小寫的,則 IgnoreCase 屬性為 False;否則為 True。缺省值為 True。
IgnoreCase 屬性的用法(改變賦予 IgnoreCase 屬性的值以觀察其效果):
RegExpTest(patrn, strng)
Dim regEx ' 建立變量。