| MAKE MONEY ONLINE How to Make Extra Money Write Articles for Hubpages Try The Keyword Academy High Income Investments Sell ClickBank Products DEVELOPER TOOLS ASP Documentation Tool .NET Documentation Tool PHP Documentation Tool SQL Documentation Tool MySQL Documentation Tool VB6 Documentation Tool Indexing Service Companion The Website Utility TECHNICAL
ARTICLES PHOTO GALLERIES TRAVEL LOG NEW STUFF MORE STUFF POPULAR STUFF LINKS |
Subexpression Substitutions in VBScript Regular ExpressionsAlthough 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 upwards. An introduction to VBScript Regular Expressions is available. Complex SubstitutionsA 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]http://www.brettb.com/[/url] .... which the bulletin board changes to: http://www.brettb.com/. Alternatively, if anchor text is required then this BBCode is used: [url=http://www.brettb.com/]BrettB.Com[/url] .... which the bulletin board changes to: BrettB.Com. Changing [url]http://www.brettb.com/[/url] to a HyperlinkTo change the first BBCode snippet to HTML, the following code can be used: <% The text replacement is achieved using the Replace method of a RegExp object. The method takes two arguments:
The regular expression specified by setting the Pattern property of the RegExp object is \[url\]([^\]]+)\[\/url\], which matches the following pattern:
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 parenthesis. 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]http://www.brettb.com/[/url] to a HyperlinkTo change [url=http://www.brettb.com/]BrettB.Com[/url] to a hyperlink with http://www.brettb.com/ as the URL and BrettB.Com as the anchor text (i.e. the HTML equivalent of <a href="http://www.brettb.com/">BrettB.Com</a>) a more complex regular expression subsitution may be used: <% In this example, there are two sets of parenthesis which match the subpattern of the URL (i.e. http://www.brettb.com) 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, "<a href=""$1"" title=""Visit $2 ($1)"">$2</a>") This replacement produces the HTML: <a href="http://www.brettb.com/" title="Visit BrettB.Com (http://www.brettb.com/)">BrettB.Com</a> Further Reading
Useful Development Tools
|
| Site Map | Privacy Policy | All content is © 1995 - 2011 Brett Burridge, hosted by Alentus |