Chapter 9. Regular Expressions

Version 5 and later of VBScript fully support regular expressions. Before that, this was one feature that was sorely lacking within VBScript, and one that made it inferior to other scripting languages, including JavaScript. This chapter begins with a brief introduction to regular expressions before taking a look at what regular expressions have to offer the programmer. It then looks at how to make use of regular expressions in VBScript code.

Introduction to Regular Expressions

Regular expressions provide powerful facilities for character pattern-matching and replacing. Before the addition of regular expressions to the VBScript engine, performing a search-and-replace task throughout a string required a fair amount of code, comprising mainly of loops, InStr, and Mid functions. Now it is possible to do all this with one line of code using a regular expression.

If you've programmed in the past using another language (C#, C++, Perl, awk, or JavaScript — even Microsoft's own JScript had support for regular expressions before VBScript did), regular expressions won't be new to you. However, one thing that experienced programmers need to know in order to leverage regular expressions in VBScript is that VBScript does not provide support for regular expression constants (like /a pattern/). Instead, VBScript uses text strings assigned to the Pattern property of a RegExp object. In many ways this is superior to the traditional method because there is no new syntax ...

Get VBScript Programmer's Reference, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.