Visual Basic Scripting Edition  

Execute 方法

对指定的字符串执行正则表达式搜索。

object.Execute(string)

参数

object
必选项。总是一个 RegExp 对象的名称。
string
必选项。要在其上执行正则表达式的文本字符串。

说明

正则表达式搜索的设计模式是通过 RegExp 对象的 Pattern 来设置的。

Execute 方法返回一个 Matches 集合,其中包含了在 string 中找到的每一个匹配的 Match 对象。如果未找到匹配,Execute 将返回空的 Matches 集合。

下面的代码说明了 Execute 方法的用法。

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 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"))

要求

版本 5

请参阅

Replace 方法 | Test 方法

应用于:RegExp 对象