復制代碼 代碼如下:
%
' --------------------------------------------------------------
' Match 對象
' 匹配搜索的結果是存放在 Match 對象中,提供了對正則表達式匹配的只讀屬性的訪問。
' Match 對象只能通過 RegExp 對象的 Execute 方法來創建,該方法實際上返回了 Match 對象的集合。
' 所有的 Match 對象屬性都是只讀的。在執行正則表達式時,可能產生零個或多個 Match 對象。
' 每個 Match 對象提供了被正則表達式搜索找到的字符串的訪問、字符串的長度,以及找到匹配的索引位置等。
' ○ FirstIndex 屬性,返回在搜索字符串中匹配的位置。FirstIndex屬性使用從零起算的偏移量,該偏移量是相對于搜索字符串的起始位置而言的。換言之,字符串中的第一個字符被標識為字符 0
' ○ Length 屬性,返回在字符串搜索中找到的匹配的長度。
' ○ Value 屬性,返回在一個搜索字符串中找到的匹配的值或文本。
' --------------------------------------------------------------
' Response.Write RegExpExecute("[ij]s.", "IS1 Js2 IS3 is4")
Function RegExpExecute(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 Value is '"
RetStr = RetStr Match.Value "'." "BR>"
Next
RegExpExecute = RetStr
End Function
' --------------------------------------------------------------------
' Replace 方法
' 替換在正則表達式查找中找到的文本。
' --------------------------------------------------------------------
' Response.Write RegExpReplace("fox", "cat") "BR>" ' 將 'fox' 替換為 'cat'。
' Response.Write RegExpReplace("(S+)(s+)(S+)", "$3$2$1") ' 交換詞對.
Function RegExpReplace(patrn, replStr)
Dim regEx, str1 ' 建立變量。
str1 = "The quick brown fox jumped over the lazy dog."
SET regEx = New RegExp ' 建立正則表達式。
regEx.Pattern = patrn ' 設置模式。
regEx.IgnoreCase = True ' 設置是否不區分大小寫。
RegExpReplace = regEx.Replace(str1, replStr) ' 作替換。
End Function
' --------------------------------------------------------------------
' 使用 Test 方法進行搜索。
' 對指定的字符串執行一個正則表達式搜索,并返回一個 Boolean 值
' 指示是否找到匹配的模式。正則表達式搜索的實際模式是通過 RegExp 對象的 Pattern 屬性來設置的。
' RegExp.Global 屬性對 Test 方法沒有影響。
' 如果找到了匹配的模式,Test 方法返回 True;否則返回 False
' --------------------------------------------------------------------
' Response.Write RegExpTest("功能", "重要功能")
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立變量。
SET regEx = New RegExp ' 建立正則表達式。
regEx.Pattern = patrn ' 設置模式。
regEx.IgnoreCase = False ' 設置是否不區分大小寫。
retVal = regEx.Test(strng) ' 執行搜索測試。
If retVal Then
RegExpTest = "找到一個或多個匹配。"
Else
RegExpTest = "未找到匹配。"
End If
End Function
%>