When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, March 15, 2000

Utilizing Regular Expressions, Part 3


Read Part 2
Read Part 1


In Part 2 we discussed how to use regular expressions to match character classes and pattern repetition. In this part, we'll look at the Replace method of the RegExp object and some finish the article off with a discussion on some of the more advanced uses of regular expressions.

- continued -

The Replace Method
If you're new to regular expressions, chances are you've always relied on the standard VBScript Replace function to replace certain substrings with other substrings. While the Replace function is easy to use, it lacks the power offered by the Replace method of the regular expression object. For example, imagine that you wanted to replace a number of offensive words in a string with less offensive variations. As we discussed earlier in this article, using the Replace function can lead to some headaches. For example, if we wished to replace all instances of "hell" with "heck" using the Replace function, we'd blindly turn "Hello" into "Hecko".

Using regular expressions, however, we can search for the word "hell" by using the special word boundary symbol. The following code snippet will replace particular words with other words (and can easily be adapted to provide a censorship type system on your site):

Dim str         ' some string that needs to have certain words
                ' replaced with other words

Dim objRegExp
Set objRegExp = New RegExp

objRegExp.IgnoreCase = True
objRegExp.Global = True

'Replace all instances of Apache with IIS
objRegExp.Pattern = "\bApache\b"
str = objRegExp.Replace(str, "IIS")

'Repalce all instances of Perl with ASP
objRegExp.Pattern = "\bPerl\b"
str = objRegExp.Replace(str, "ASP")

Set objRegExp = Nothing        'Clean up!

Note that the Replace method takes two parameters: the first is the string to search; the second is the text to replace any matches with. The Replace method returns the modified string.

Advanced Uses of Regular Expressions
One of the best uses of regular expressions is for server-side form validation. I have not been able to find many articles on server-side form validation using regular expression using VBScript. Those only article I found was one here on 4Guys:

I did, however, find a number of articles on client-side validation with regular expression and server-side validation with regular expression.

There are, of course, other uses for regular expression. If you have some good, practical uses of regular expression, and would like to share them with the 4Guys audience, please send me your ideas!

Happy Programming!

  • Read Part 2
  • Read Part 1


  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article