Subexpression Substitutions in VBScript RegExp
page 1 of 1
Published: 28 Oct 2004
Unedited - Community Contributed
This article shows how to make complex substitutions in VBScript using the RegExp regular expressions object.
by Brett Burridge
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23852/ 7

VBScript Regular Expressions

Although there are a good range of text handling functions in VBScript, there are times when the required text replacement operation would be very tricky to carry out with a combination of calls to functions such as Replace, InStr, and Left. In these cases, it is worth considering the use of VBScript's regular expressions, which are supported from VBScript Version 5 and upwards. An Introduction to VBScript Regular Expressions is available.

Complex Substitutions

A typical use of regular expression substitutions is to process the BBCode tags into HTML. BBCode is a simplified version of HTML used by web-based applications such as the phpBB bulletin board. For example, if the user wishes to insert a hyperlink in their message then they can either use the BBCode:

[url][/url]   .... which the bulletin board changes to:

Alternatively, if anchor text is required then this BBCode is used:

[url=]BrettB.Com[/url]   .... which the bulletin board changes to: BrettB.Com.

Changing [url][/url] to a Hyperlink

To change the first BBCode snippet to HTML, the following code can be used:

InitialString = "[url][/url]"

Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = "\[url\]([^\]]+)\[\/url\]"
.IgnoreCase = True
.Global = True
End With

ReplacedString = RegularExpressionObject.Replace(InitialString, "$1")

Response.Write "Replaced " & InitialString & " with: " & ReplacedString

The text replacement is achieved using the Replace method of a RegExp object. The method takes two arguments:

  • The first argument is the string to be searched.
  • The second argument is the replacement to be made.

The regular expression specified by setting the Pattern property of the RegExp object is \[url\]([^\]]+)\[\/url\], which matches the following pattern:

  • \[ = Matches an opening square bracket.
  • url = Matches the text url.
  • \] = Matches a closing square bracket.
  • ([^\]]+) = Match one or more of any character except for the character enclosed in square brackets, which in this instance is a closing square bracket. This part of the pattern (which matches the URL between the BBCode [url] ... [/url] tags) is enclosed in parenthesis because it needs to be remembered for the substitution.
  • \[\/url\] = Matches the closing BBCode url tag.

Note that because square brackets ([ ]) and forward slashes (/) have special meanings in regular expressions, they have to be "escaped" by prefixing them with a backward slash (\).

In this example, the URL specified between the BBCode [url] ... [/url] tags is "remembered" because it is within a set of parentheses. As a result, it can be used in the replacement by using a dollar sign immediately followed by the number of the subpattern in the regular expression.

There can be more than one set of parenthesis in a regular expression pattern. This makes it possible to reorder text in substitutions - see the next section for more details of this.

Changing [url][/url] to a Hyperlink

To change [url=]BrettB.Com[/url] to a hyperlink with as the URL and BrettB.Com as the anchor text (i.e. the HTML equivalent of <a href="">BrettB.Com</a>) a more complex regular expression subsitution may be used:

InitialString = "[url=]BrettB.Com[/url]"

Set RegularExpressionObject = New RegExp

With RegularExpressionObject
.Pattern = "\[url=([^\]]+)\]([^\]]+)\[\/url\]"
.IgnoreCase = True
.Global = True
End With

ReplacedString = RegularExpressionObject.Replace(InitialString, "$2")

Response.Write "Replaced " & InitialString & " with: " & ReplacedString

Set RegularExpressionObject = nothing

In this example, there are two sets of parentheses that match the subpattern of the URL (i.e. and the anchor text (i.e. BrettB.Com). These are accessible in the RegExp object's Replace method as $1 and $2, respectively.

As mentioned previously, these patterns may be used in a different order to the way they were matched. They can also be used more than once. For example, this line adds additional HTML to include a title attribute for the a tag:

ReplacedString = RegularExpressionObject.Replace(InitialString, "$2")

This replacement produces the HTML:

<a href="" title="Visit BrettB.Com (">BrettB.Com</a>

Further Reading

  • Microsoft Beefs Up VBScript with Regular Expressions
  • Having trouble with your (or somebody else's source code?) The ASP Documentation Toolâ„¢ creates developer documentation for ASP written in VBScript and JScript. Documentation for Microsoft Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the web application can also be incorporated into the reports. Documentation is created in HTML, HTML Help and plain text formats.

User Comments

Title: VBScript Regular Expressions   
Name: Rob Meade
Date: 2005-04-05 3:29:39 PM

Thanks very much for this article, I am just about to try some of your examples. I wrote a content management system over 2 years ago and had a basic set of tags that could be used, I was using replace statements which had worked well until starting to add more complex requirements...

One thing I would be interested in seeing added to your article would be an example of changing it back again - in your article you suggest that the common requirement is for replacing bbcode - obviously in a forum application you can 'edit' your messages, you would then want to be able to reverse your regex I guess and produce the [url= etc etc..

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2017  |  Page Processed at 2017-01-21 2:37:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search