RegExp對象的用法: RegExpTest(patrn, strng) Dim regEx, Match, Matches ' 建立變量。 Set regEx = New RegExp ' 建立正則表達式。 regEx.Pattern = patrn ' 設(shè)置模式。 regEx.IgnoreCase = True ' 設(shè)置是否區(qū)分字符大小寫。 regEx.Global = True ' 設(shè)置全局可用性。 Set Matches = regEx.Execute(strng) ' 執(zhí)行搜索。 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
eplace 方法的用法: ReplaceTest(patrn, replStr) Dim regEx, str1 ' 建立變量。 str1 = "The quick brown fox jumped over the lazy dog." Set regEx = New RegExp ' 建立正則表達式。 regEx.Pattern = patrn ' 設(shè)置模式。 regEx.IgnoreCase = True ' 設(shè)置是否區(qū)分大小寫。 ReplaceTest = regEx.Replace(str1, replStr) ' 作替換。 End
Test 方法的用法: RegExpTest(patrn, strng) Dim regEx, retVal ' 建立變量。 Set regEx = New RegExp ' 建立正則表達式。 regEx.Pattern = patrn ' 設(shè)置模式。 regEx.IgnoreCase = False ' 設(shè)置是否區(qū)分大小寫。 retVal = regEx.Test(strng) ' 執(zhí)行搜索測試。 If retVal Then RegExpTest = "找到一個或多個匹配。" Else RegExpTest = "未找到匹配。" End If End