ASP中RegExp对象的Execute方法与Match对象详解
说明
Match 对象无法直接实例化,必须借助 RegExp 对象的 Execute 方法生成,该方法的返回值即为 Match 对象的集合。此集合中所有 Match 对象的属性均处于只读状态。
执行正则表达式匹配时,系统可能生成零个或多个 Match 对象。每个对象都封装了匹配信息,允许访问被搜索字符串的内容、长度以及匹配发生的起始索引位置等数据。
以下代码展示了 Match 对象的具体应用方式:
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 = RetStr & "Match " & I & " found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is "'
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))