RegExp 对象的属性和方法

一、RegExp 对象的属性

1. Global属性

说明:Global属性设置或返回一个 Boolean 值,该值指明在整个搜索字符串时模式是全部匹配还是只匹配第一个。
语法:object.Global [= True | False ]
语法描述:
● 其中object 参数总是 RegExp 对象。如果搜索应用于整个字符串,Global 属性的值为 True,否则其值为 False。默认的设置为 True。
运用举例:
Function RegExpTest(patrn, strng)
Dim regEx ' 建立变量。
Set regEx = New RegExp ' 建立规范表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分字母的大小写。
regEx.Global = True ' 设置全程性质。
RegExpTest = regEx.Execute(strng) ' 执行搜索。
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

2. IgnoreCase属性

说明:IgnoreCase属性设置或返回一个Boolean值,指明模式搜索是否区分大小写。
语法:object.IgnoreCase [= True | False ]
语法描述:
● object 参数总是一个 RegExp 对象。如果搜索是区分大小写的,则 IgnoreCase 属性为 False,否则为 True。缺省值为 True。
运用举例:
Function RegExpTest(patrn, strng)
Dim regEx ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
RegExpTest = regEx.Execute(strng) ' 执行搜索。
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

3. Pattern属性

说明:Pattern属性设置或返回被搜索的正则表达式模式。
语法:object.Pattern [= "searchstring"]
语法描述:
● object 必需的。总是一个 RegExp 对象变量。
● searchstring 可选的。被搜索的正则字符串表达式。它可能包含设置部分表格中的各种正则表达式字符。
运用举例:
Function RegExpTest(patrn, strng)
Dim regEx ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
RegExpTest = regEx.Execute(strng) ' 执行搜索。
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

二、RegExp对象的方法

1. Execute方法

说明:Execute方法对指定的字符串执行正则表达式搜索。正则表达式搜索的设计模式是通过 RegExp 对象的 Pattern 来设置的。Execute 方法返回一个 Matches 集合,其中包含了在 string 中找到的每一个匹配的 Match 对象。如果未找到匹配,Execute 将返回空的 Matches 集合。
语法:object.Execute(string)
语法描述:
● object 必需的。总是一个 RegExp 对象的名称。
● string 必需的。要在其上执行正则表达式的文本字符串。
运用举例:
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 ' 遍历匹配集合。
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"))

2. Replace方法

说明:Replace方法替换在正则表达式查找中找到的文本。被替换的文本的实际模式是通过 RegExp 对象的 Pattern 属性设置的。Replace 方法返回 string1 的副本,其中的 RegExp.Pattern 文本已经被替换为 string2。如果没有找到匹配的文本,将返回原来的 string1 的副本。
语法:object.Replace(string1, string2)
语法描述:
● object 必需的。总是一个 RegExp 对象的名称。
● string1 必需的。string1 是将要进行文本替换的字符串。
● string2 必需的。 string2 是替换文本字符串。
运用举例:
Function ReplaceTest(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 ' 设置是否区分大小写。
ReplaceTest = regEx.Replace(str1, replStr) ' 作替换。
End Function
MsgBox(ReplaceTest("fox", "cat"))

3. Test方法

说明:Test方法对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。正则表达式搜索的实际模式是通过RegExp对象的Pattern属性来设置的。RegExp.Global属性对Test方法没有影响。如果找到了匹配的模式,Test方法返回True;否则返回False。
语法:object.Test(string)
语法描述:
● object 必需的。总是一个 RegExp 对象的名称。
● string 必需的。要执行正则表达式搜索的文本字符串。
运用举例:
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
MsgBox(RegExpTest("ow", "windowXP"))

评论: 1 | 引用: 0 | 查看次数: 4197
发表评论
登录后再发表评论!