Replacemethod of the
RegExpobject and some finish the article off with a discussion on some of the more advanced uses of regular expressions.
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
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):
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
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.
- Validating Email Addresses Using JScript and Regular Expression
- A Library of Regular Expression for Form Validation
- Validation Made Simple with JScript Regular Expressions
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!