One potential application for regular expressions in VBScript is to process text entered into a website via a form. Normal text replacement will allow you to filter out swear words and highlight specific phrases, but regular expressions allow you to go further. The below example demonstrates how to use them to make any valid email addresses or URLs into clickable links programmatically.
The function itself is easy to call, like so:
strTextToProcess = create_links(strTextToProcess)The create_links function makes use of another function, also included below, called "ereg_replace". This is a simple function to make regular expression text replacement easier, and you can find out more about it in my article about VBScript Regular Expressions.
function create_links(strText)
strText = " " & strText
strText = ereg_replace(strText, "(^|[\n ])([\w]+?://[^ ,""\s<]*)", "$1<a href=""$2"">$2</a>")
strText = ereg_replace(strText, "(^|[\n ])((www|ftp)\.[^ ,""\s<]*)", "$1<a href=""http://$2"">$2</a>")
strText = ereg_replace(strText, "(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)", "$1<a href=""mailto:$2@$3"">$2@$3</a>")
strText = right(strText, len(strText)-1)
create_links = strText
end function
function ereg_replace(strOriginalString, strPattern, strReplacement)
' Function replaces pattern with replacement
dim objRegExp : set objRegExp = new RegExp
objRegExp.Pattern = strPattern
objRegExp.IgnoreCase = True
objRegExp.Global = True
ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
set objRegExp = nothing
end functionFinally, here is a demonstration of the above code in action:
strTextToProcess = "This simple pair of functions, from http://www.ilovejackdaniels.com, will take any text and convert valid URLs and email addresses into clickable links. Problems, feedback and suggestions should be sent to dave@ilovejackdaniels.com or posted in the comments section, which you can reach through the link below."
strTextToProcess = create_links(strTextToProcess)
response.write strTextToProcessThe three lines above will output:
This simple pair of functions, from http://www.ilovejackdaniels.com, will take any text and convert valid URLs and email addresses into clickable links. Problems, feedback and suggestions should be sent to dave@ilovejackdaniels.com or posted in the comments section, which you can reach through the link below.
Tags
Syndication
If you like this post, subscribe to my full feed or partial feed.

ILoveJackDaniels.com is the online playground of