VBScript 的正则表达式支持VBScript 的正则表达式支持VBScript 的正则表达式支持VBScript 的正则表达式支持
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
VBScript 的正则表达式支持
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » VBScript 的正则表达式支持

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
本网站的更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

VBScript 的正则表达式支持

VBScript 内置支持正则表达式。如果您使用 VBScript 在用户端验证网页上的用户输入,使用 VBScript 的正则表达式支持将大幅减少您需要编写的代码量。

Microsoft 在 Internet Explorer 版本 5.5 中对 VBScript 的正则表达式支持进行了一些重大强化。版本 5.5 实作了相当多在先前版本 VBScript 中遗漏的必要正则表达式功能。每当本网站提到 VBScript 时,这些陈述都指的是 VBScript 版本 5.5 的正则表达式支持。

基本上,Internet Explorer 5.5 实作 JavaScript 正则表达式风格。但 IE 5.5 在网页标准方面的得分并不高。在它实作的 JavaScript 正则表达式和实际标准之间有相当多的差异。很幸运地,大部分都是不太可能影响您的边缘案例。因此,本网站上关于 JavaScript 正则表达式风格所述的一切也适用于 VBScript。现代版本的 IE 在奇异模式中呈现网页时仍然使用 IE 5.5 实作。在标准模式中,现代版本的 IE 非常严格地遵循 JavaScript 标准。即使已安装现代版本的 IE,VBScript 正则表达式仍然使用 IE 5.5 实作。

JavaScript 和 VBScript 实作 Perl 风格的正则表达式。然而,它们缺少许多 Perl 和其他现代正则表达式风格中可用的高端功能

  • 没有 \A 或 \Z锚定 来比对字符串的开头或结尾。改用 插入符号或美元符号。
  • 后向观察 完全不支持。 前向观察 完全支持。
  • 没有 原子组 或 独占量词
  • 没有 Unicode 支持,除了使用 \uFFFF 比对单一字符
  • 没有 命名捕获组。改用 编号捕获组。
  • 没有 模式修改词 来设置正则表达式中的比对选项。
  • 没有 条件式。
  • 没有 正则表达式注解。改用 VBScript 单引号注解来描述正则表达式,放在正则表达式字符串之外。

RegExp 对象的 1.0 版甚至缺少 惰性量词 等基本功能。这是本网站不讨论 VBScript RegExp 1.0 的主要原因。所有早于 5.5 版的 Internet Explorer 都包含 1.0 版的 RegExp 对象。除了 1.0 和 5.5 版之外,没有其他版本。

如何使用 VBScript RegExp 对象

你可以通过创建一个或多个 RegExp 对象的运行个体来在 VBScript 中使用正则表达式。这个对象让你可以在字符串中寻找正则表达式比对,并用其他字符串取代字符串中的正则表达式比对。VBScript 的 RegExp 对象提供的功能相当阳春。然而,对于 VBScript 中通常运行的简单输入验证和输出格式化任务来说,已经绰绰有余。

RegExp 对象阳春特性的优点是它非常容易使用。创建一个,放入正则表达式,然后让它比对或取代。只有四个属性和三个方法可用。

创建对象后,将你想要搜索的正则表达式指定给 Pattern 属性。如果你想要使用文本正则表达式,而不是用户提供的正则表达式,只要将正则表达式放在双引号字符串中即可。缺省情况下,正则表达式会区分大小写。将 IgnoreCase 属性设置为 True 以不区分大小写。 插入符号和美元符号 缺省只会比对主旨字符串的最开头和最结尾。如果你的主旨字符串包含多行,由换行符号分隔,你可以通过将 Multiline 属性设置为 True,让插入符号和美元符号 比对这些行的开头和结尾。VBScript 没有 让点符号比对换行符号字符 的选项。最后,如果你想要 RegExp 对象传回或取代所有比对,而不是只传回或取代第一个比对,请将 Global 属性设置为 True。

'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"

设置 RegExp 对象的属性后,你可以调用三个方法中的其中一个来运行三个基本任务之一。 Test 方法接收一个参数:要对其测试正则表达式的字符串。 Test 传回 True 或 False,表示正则表达式是否比对(字符串的一部分)。在验证用户输入时,你通常会想要检查整个字符串是否比对正则表达式。为此,请在正则表达式的开头放置 插入符号,在结尾放置 美元符号,将正则表达式锚定在主旨字符串的开头和结尾。

运行方法也采用一个字符串参数。它不会传回 True 或 False,而是传回一个 MatchCollection 对象。如果正则表达式完全无法与主旨字符串相符,MatchCollection.Count 会为零。如果 RegExp.Global 属性为 False(缺省),MatchCollection 将只包含第一个相符项目。如果 RegExp.Global 为 true,Matches> 将包含所有相符项目。

取代方法采用两个字符串参数。第一个参数为主旨字符串,而第二个参数为取代文本。如果 RegExp.Global 属性为 False(缺省),取代 将传回主旨字符串,其中第一个正则表达式相符项目(如果有的话)已用取代文本取代。如果 RegExp.Global 为 true,取代 将传回主旨字符串,其中所有正则表达式相符项目都已取代。

您可以指定一个空白字符串作为取代文本。这将导致 取代 方法传回主旨字符串,其中已删除所有正则表达式相符项目。若要将正则表达式相符项目重新插入为取代项目的一部分,请在取代文本中包含 $&。例如,若要在字符串中用方括号括住每个正则表达式相符项目,请指定 [$&] 作为取代文本。如果正则表达式包含 截取括号,您可以在取代文本中使用 反向引用。取代文本中的 $1 会插入与第一个捕获组相符的文本,$2 会插入第二个相符群组,依此类推,直到 $9。若要在取代项目中包含一个实际的美元符号,请在传递给 取代 方法的字符串中放置两个连续的美元符号。

取得个别相符项目的信息

RegExp.Execute 方法传回的 MatchCollection 对象是 Match 对象的集合。它只有两个唯读属性。Count 属性指出集合包含多少个相符项目。Item 属性采用一个索引参数(范围从零到 Count-1),并传回一个 Match 对象。Item 属性是缺省成员,因此您可以将 MatchCollection(7) 写成 MatchCollection.Item(7) 的简写。

处理集合中所有相符项目的最简单方法是使用 For Each 建构,例如

' Pop up a message box for each match
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  msgbox myMatch.Value, 0, "Found Match"
Next

Match 对象有四个唯读属性。FirstIndex 属性指出相符项目左方字符串中的字符数。如果相符项目是在字符串的最开头找到,FirstIndex 会为零。如果相符项目从字符串中的第二个字符开始,FirstIndex 会为一,依此类推。请注意,这与 VBScript Mid 函数不同,后者会在您将 start 参数设置为一的情况下截取字符串的第一个字符。Match 对象的 Length 属性指出相符项目中的字符数。Value 属性传回相符的文本。

**SubMatches** 属性为 Match 对象的字符串集合。只有当您的正则表达式有 **捕获组** 时,它才会保留值。该集合会为每个捕获组保留一个字符串。**Count** 属性会指出集合中的字符串数目。**Item** 属性会取得一个索引参数,并传回由捕获组比对的文本。**Item** 属性为缺省成员,因此您可以将 **SubMatches(7)** 写成 **SubMatches.Item(7)** 的缩写。很遗憾地,VBScript 没有提供任何方式来截取捕获组的比对位置和长度。

另一个遗憾的是,**SubMatches** 属性**没有**保留完整的正则表达式比对,例如 **SubMatches(0)**。相反地,**SubMatches(0)** 会保留由第一个捕获组比对的文本,而 **SubMatches(SubMatches.Count-1)** 会保留由最后一个捕获组比对的文本。这与大多数其他编程语言不同。例如在 **VB.NET** 中,**Match.Groups(0)** 会传回整个正则表达式比对,而 **Match.Groups(1)** 会传回第一个捕获组的比对。请注意,这也与您可以在传递给 **RegExp.Replace** 方法的取代文本中使用的反向引用不同。在取代文本中,**$1** 会插入由第一个捕获组比对的文本,就像大多数其他正则表达式风格一样。**$0** 没有被任何东西取代,而是直接插入。

在您的网络浏览器中测试 VBScript 的 RegExp 支持

我创建了一个 **范例网页,显示 VBScript 的正则表达式支持** 的实际应用。如果您使用的是 Internet Explorer,您现在可以在您的网络浏览器中尝试看看。原代码显示在范例下方。

VBScript 的正則表示式支援
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » VBScript 的正則表示式支援

正則表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
本網站的更多內容
簡介
正則表示式快速入門
正則表示式教學
替換字串教學
應用程式和語言
正則表示式範例
正則表示式參考
替換字串參考

VBScript 的正則表示式支援

VBScript 內建支援正則表示式。如果您使用 VBScript 在用戶端驗證網頁上的使用者輸入,使用 VBScript 的正則表示式支援將大幅減少您需要編寫的程式碼量。

Microsoft 在 Internet Explorer 版本 5.5 中對 VBScript 的正則表示式支援進行了一些重大強化。版本 5.5 實作了相當多在先前版本 VBScript 中遺漏的必要正則表示式功能。每當本網站提到 VBScript 時,這些陳述都指的是 VBScript 版本 5.5 的正則表示式支援。

基本上,Internet Explorer 5.5 實作 JavaScript 正則表示式風味。但 IE 5.5 在網頁標準方面的得分並不高。在它實作的 JavaScript 正則表示式和實際標準之間有相當多的差異。很幸運地,大部分都是不太可能影響您的邊緣案例。因此,本網站上關於 JavaScript 正則表示式風味所述的一切也適用於 VBScript。現代版本的 IE 在奇異模式中呈現網頁時仍然使用 IE 5.5 實作。在標準模式中,現代版本的 IE 非常嚴格地遵循 JavaScript 標準。即使已安裝現代版本的 IE,VBScript 正則表示式仍然使用 IE 5.5 實作。

JavaScript 和 VBScript 實作 Perl 風格的正規表示式。然而,它們缺少許多 Perl 和其他現代正規表示式風格中可用的進階功能

  • 沒有 \A 或 \Z錨定 來比對字串的開頭或結尾。改用 插入符號或美元符號。
  • 後向觀察 完全不支援。 前向觀察 完全支援。
  • 沒有 原子群組 或 獨佔量詞
  • 沒有 Unicode 支援,除了使用 \uFFFF 比對單一字元
  • 沒有 命名擷取群組。改用 編號擷取群組。
  • 沒有 模式修改詞 來設定正規表示式中的比對選項。
  • 沒有 條件式。
  • 沒有 正規表示式註解。改用 VBScript 單引號註解來描述正規表示式,放在正規表示式字串之外。

RegExp 物件的 1.0 版甚至缺少 惰性量詞 等基本功能。這是本網站不討論 VBScript RegExp 1.0 的主要原因。所有早於 5.5 版的 Internet Explorer 都包含 1.0 版的 RegExp 物件。除了 1.0 和 5.5 版之外,沒有其他版本。

如何使用 VBScript RegExp 物件

你可以透過建立一個或多個 RegExp 物件的執行個體來在 VBScript 中使用正規表示式。這個物件讓你可以在字串中尋找正規表示式比對,並用其他字串取代字串中的正規表示式比對。VBScript 的 RegExp 物件提供的功能相當陽春。然而,對於 VBScript 中通常執行的簡單輸入驗證和輸出格式化任務來說,已經綽綽有餘。

RegExp 物件陽春特性的優點是它非常容易使用。建立一個,放入正規表示式,然後讓它比對或取代。只有四個屬性和三個方法可用。

建立物件後,將你想要搜尋的正規表示式指定給 Pattern 屬性。如果你想要使用文字正規表示式,而不是使用者提供的正規表示式,只要將正規表示式放在雙引號字串中即可。預設情況下,正規表示式會區分大小寫。將 IgnoreCase 屬性設定為 True 以不區分大小寫。 插入符號和美元符號 預設只會比對主旨字串的最開頭和最結尾。如果你的主旨字串包含多行,由換行符號分隔,你可以透過將 Multiline 屬性設定為 True,讓插入符號和美元符號 比對這些行的開頭和結尾。VBScript 沒有 讓點符號比對換行符號字元 的選項。最後,如果你想要 RegExp 物件傳回或取代所有比對,而不是只傳回或取代第一個比對,請將 Global 屬性設定為 True。

'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"

設定 RegExp 物件的屬性後,你可以呼叫三個方法中的其中一個來執行三個基本任務之一。 Test 方法接收一個參數:要對其測試正規表示式的字串。 Test 傳回 True 或 False,表示正規表示式是否比對(字串的一部分)。在驗證使用者輸入時,你通常會想要檢查整個字串是否比對正規表示式。為此,請在正規表示式的開頭放置 插入符號,在結尾放置 美元符號,將正規表示式錨定在主旨字串的開頭和結尾。

執行方法也採用一個字串參數。它不會傳回 True 或 False,而是傳回一個 MatchCollection 物件。如果正規表示式完全無法與主旨字串相符,MatchCollection.Count 會為零。如果 RegExp.Global 屬性為 False(預設),MatchCollection 將只包含第一個相符項目。如果 RegExp.Global 為 true,Matches> 將包含所有相符項目。

取代方法採用兩個字串參數。第一個參數為主旨字串,而第二個參數為取代文字。如果 RegExp.Global 屬性為 False(預設),取代 將傳回主旨字串,其中第一個正規表示式相符項目(如果有的話)已用取代文字取代。如果 RegExp.Global 為 true,取代 將傳回主旨字串,其中所有正規表示式相符項目都已取代。

您可以指定一個空白字串作為取代文字。這將導致 取代 方法傳回主旨字串,其中已刪除所有正規表示式相符項目。若要將正規表示式相符項目重新插入為取代項目的一部分,請在取代文字中包含 $&。例如,若要在字串中用方括號括住每個正規表示式相符項目,請指定 [$&] 作為取代文字。如果正規表示式包含 擷取括號,您可以在取代文字中使用 反向參照。取代文字中的 $1 會插入與第一個擷取群組相符的文字,$2 會插入第二個相符群組,依此類推,直到 $9。若要在取代項目中包含一個實際的美元符號,請在傳遞給 取代 方法的字串中放置兩個連續的美元符號。

取得個別相符項目的資訊

RegExp.Execute 方法傳回的 MatchCollection 物件是 Match 物件的集合。它只有兩個唯讀屬性。Count 屬性指出集合包含多少個相符項目。Item 屬性採用一個索引參數(範圍從零到 Count-1),並傳回一個 Match 物件。Item 屬性是預設成員,因此您可以將 MatchCollection(7) 寫成 MatchCollection.Item(7) 的簡寫。

處理集合中所有相符項目的最簡單方法是使用 For Each 建構,例如

' Pop up a message box for each match
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  msgbox myMatch.Value, 0, "Found Match"
Next

Match 物件有四個唯讀屬性。FirstIndex 屬性指出相符項目左方字串中的字元數。如果相符項目是在字串的最開頭找到,FirstIndex 會為零。如果相符項目從字串中的第二個字元開始,FirstIndex 會為一,依此類推。請注意,這與 VBScript Mid 函數不同,後者會在您將 start 參數設定為一的情況下擷取字串的第一個字元。Match 物件的 Length 屬性指出相符項目中的字元數。Value 屬性傳回相符的文字。

**SubMatches** 屬性為 Match 物件的字串集合。只有當您的正規表示式有 **擷取群組** 時,它才會保留值。該集合會為每個擷取群組保留一個字串。**Count** 屬性會指出集合中的字串數目。**Item** 屬性會取得一個索引參數,並傳回由擷取群組比對的文字。**Item** 屬性為預設成員,因此您可以將 **SubMatches(7)** 寫成 **SubMatches.Item(7)** 的縮寫。很遺憾地,VBScript 沒有提供任何方式來擷取擷取群組的比對位置和長度。

另一個遺憾的是,**SubMatches** 屬性**沒有**保留完整的正規表示式比對,例如 **SubMatches(0)**。相反地,**SubMatches(0)** 會保留由第一個擷取群組比對的文字,而 **SubMatches(SubMatches.Count-1)** 會保留由最後一個擷取群組比對的文字。這與大多數其他程式語言不同。例如在 **VB.NET** 中,**Match.Groups(0)** 會傳回整個正規表示式比對,而 **Match.Groups(1)** 會傳回第一個擷取群組的比對。請注意,這也與您可以在傳遞給 **RegExp.Replace** 方法的取代文字中使用的反向參照不同。在取代文字中,**$1** 會插入由第一個擷取群組比對的文字,就像大多數其他正規表示式風格一樣。**$0** 沒有被任何東西取代,而是直接插入。

在您的網路瀏覽器中測試 VBScript 的 RegExp 支援

我建立了一個 **範例網頁,顯示 VBScript 的正規表示式支援** 的實際應用。如果您使用的是 Internet Explorer,您現在可以在您的網路瀏覽器中嘗試看看。原始碼顯示在範例下方。

VBScript’s Regular Expression Support
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » VBScript’s Regular Expression Support

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

VBScript’s Regular Expression Support

VBScript has built-in support for regular expressions. If you use VBScript to validate user input on a web page at the client side, using VBScript’s regular expression support will greatly reduce the amount of code you need to write.

Microsoft made some significant enhancements to VBScript’s regular expression support in version 5.5 of Internet Explorer. Version 5.5 implements quite a few essential regex features that were missing in previous versions of VBScript. Whenever this website mentions VBScript, the statements refer to VBScript’s version 5.5 regular expression support.

Basically, Internet Explorer 5.5 implements the JavaScript regular expression flavor. But IE 5.5 did not score very high on web standards. There are quite a few differences between its implementation of JavaScript regular expressions and the actual standard. Fortunately, most are corner cases that are not likely to affect you. Therefore, everything said about JavaScript’s regular expression flavor on this website also applies to VBScript. Modern versions of IE still use the IE 5.5 implementation when rendering web pages in quirks mode. In standards mode, modern versions of IE follow the JavaScript standard very closely. VBScript regular expressions also still use the IE 5.5 implementation, even when a modern version of IE is installed.

JavaScript and VBScript implement Perl-style regular expressions. However, they lack quite a number of advanced features available in Perl and other modern regular expression flavors:

  • No \A or \Z anchors to match the start or end of the string. Use a caret or dollar instead.
  • Lookbehind is not supported at all. Lookahead is fully supported.
  • No atomic grouping or possessive quantifiers
  • No Unicode support, except for matching single characters with \uFFFF
  • No named capturing groups. Use numbered capturing groups instead.
  • No mode modifiers to set matching options within the regular expression.
  • No conditionals.
  • No regular expression comments. Describe your regular expression with VBScript apostrophe comments instead, outside the regular expression string.

Version 1.0 of the RegExp object even lacks basic features like lazy quantifiers. This is the main reason this website does not discuss VBScript RegExp 1.0. All versions of Internet Explorer prior to 5.5 include version 1.0 of the RegExp object. There are no other versions than 1.0 and 5.5.

How to Use the VBScript RegExp Object

You can use regular expressions in VBScript by creating one or more instances of the RegExp object. This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings. The functionality offered by VBScript’s RegExp object is pretty much bare bones. However, it’s more than enough for simple input validation and output formatting tasks typically done in VBScript.

The advantage of the RegExp object’s bare-bones nature is that it’s very easy to use. Create one, put in a regex, and let it match or replace. Only four properties and three methods are available.

After creating the object, assign the regular expression you want to search for to the Pattern property. If you want to use a literal regular expression rather than a user-supplied one, simply put the regular expression in a double-quoted string. By default, the regular expression is case sensitive. Set the IgnoreCase property to True to make it case insensitive. The caret and dollar only match at the very start and very end of the subject string by default. If your subject string consists of multiple lines separated by line breaks, you can make the caret and dollar match at the start and the end of those lines by setting the Multiline property to True. VBScript does not have an option to make the dot match line break characters. Finally, if you want the RegExp object to return or replace all matches instead of just the first one, set the Global property to True.

'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"

After setting the RegExp object’s properties, you can invoke one of the three methods to perform one of three basic tasks. The Test method takes one parameter: a string to test the regular expression on. Test returns True or False, indicating if the regular expression matches (part of) the string. When validating user input, you’ll typically want to check if the entire string matches the regular expression. To do so, put a caret at the start of the regex, and a dollar at the end, to anchor the regex at the start and end of the subject string.

The Execute method also takes one string parameter. Instead of returning True or False, it returns a MatchCollection object. If the regex could not match the subject string at all, MatchCollection.Count will be zero. If the RegExp.Global property is False (the default), MatchCollection will contain only the first match. If RegExp.Global is true, Matches> will contain all matches.

The Replace method takes two string parameters. The first parameter is the subject string, while the second parameter is the replacement text. If the RegExp.Global property is False (the default), Replace will return the subject string with the first regex match (if any) substituted with the replacement text. If RegExp.Global is true, Replace will return the subject string with all regex matches replaced.

You can specify an empty string as the replacement text. This will cause the Replace method to return the subject string with all regex matches deleted from it. To re-insert the regex match as part of the replacement, include $& in the replacement text. E.g. to enclose each regex match in the string between square brackets, specify [$&] as the replacement text. If the regexp contains capturing parentheses, you can use backreferences in the replacement text. $1 in the replacement text inserts the text matched by the first capturing group, $2 the second, etc. up to $9. To include a literal dollar sign in the replacements, put two consecutive dollar signs in the string you pass to the Replace method.

Getting Information about Individual Matches

The MatchCollection object returned by the RegExp.Execute method is a collection of Match objects. It has only two read-only properties. The Count property indicates how many matches the collection holds. The Item property takes an index parameter (ranging from zero to Count-1), and returns a Match object. The Item property is the default member, so you can write MatchCollection(7) as a shorthand to MatchCollection.Item(7).

The easiest way to process all matches in the collection is to use a For Each construct, e.g.:

' Pop up a message box for each match
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  msgbox myMatch.Value, 0, "Found Match"
Next

The Match object has four read-only properties. The FirstIndex property indicates the number of characters in the string to the left of the match. If the match was found at the very start of the string, FirstIndex will be zero. If the match starts at the second character in the string, FirstIndex will be one, etc. Note that this is different from the VBScript Mid function, which extracts the first character of the string if you set the start parameter to one. The Length property of the Match object indicates the number of characters in the match. The Value property returns the text that was matched.

The SubMatches property of the Match object is a collection of strings. It will only hold values if your regular expression has capturing groups. The collection will hold one string for each capturing group. The Count property indicates the number of string in the collection. The Item property takes an index parameter, and returns the text matched by the capturing group. The Item property is the default member, so you can write SubMatches(7) as a shorthand to SubMatches.Item(7). Unfortunately, VBScript does not offer a way to retrieve the match position and length of capturing groups.

Also unfortunately is that the SubMatches property does not hold the complete regex match as SubMatches(0). Instead, SubMatches(0) holds the text matched by the first capturing group, while SubMatches(SubMatches.Count-1) holds the text matched by the last capturing group. This is different from most other programming languages. E.g. in VB.NET, Match.Groups(0) returns the whole regex match, and Match.Groups(1) returns the first capturing group’s match. Note that this is also different from the backreferences you can use in the replacement text passed to the RegExp.Replace method. In the replacement text, $1 inserts the text matched by the first capturing group, just like most other regex flavors do. $0 is not substituted with anything but inserted literally.

Test VBScript’s RegExp Support In Your Web Browser

I have created an example web page showing VBScript’s regex support in action. If you’re using Internet Explorer, you can try it right now in your web browser. Source code is displayed below the example.

©2015-2025 艾丽卡 support@alaica.com