Skip Navigation

Make URLs and Email Addresses Clickable Automatically with VBScript

 
VBScript Regular Expressions are, as in other languages, a very powerful tool, allowing you to find and manipulate patterns within strings easily and quickly. The syntax for them can often be a headache, but once you are familiar with them, you will find them invaluable.

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 function

Finally, 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 strTextToProcess

The 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.

 

16 comments (Add Yours)

You can keep up to date with this discussion by subscribing to the RSS or Atom feed.
 
matroxjr
Malaysia #1: July 12, 2004
hi jack daniels.. i tried your code to make URLs Email Address Clickable Automatically .. it working.. and nice code.. but i have one more problem to discuss. The problem is how to open the hyperlinks address as a blank document or as new page.. can we discuss about this?
Good question. On the third line of the create_links() function is a small part that says "$1<a href=""$2"">$2</a>". Just replace that with "$1<a target="_blank" href=""$2"">$2</a>".
Nisse
Sweden #3: July 16, 2004
Hi ILJD!
Great script! But there's a little bug that I don't think is intended...

If I post an url on a new row, like this:
http://nuss.se/
The url doesn't get "urlified"! The regexp seems to require a space before the url.
Curious. Thanks, Nisse, for pointing that out. I've updated the code slightly so it should work for any whitespace, including new lines (it should have done before, strangely).
Thanks for a great script. However I got a problem. If the URL is at the end of a sentence, the dot is included in the link. Which leads to a faulty link.
Yes - unfortunately, that is tricky to fix, as periods are legitimate characters in URLs. I'm working on a better version of this function though, and fixing that bug is one of the aims of the new version.
Bas
Netherlands #7: February 18, 2005
#4. ILoveJackDaniels on July 16, 2004:

Curious. Thanks, Nisse, for pointing that out. I've updated the code slightly so it should work for any whitespace, including new lines (it should have done before, strangely).

I still seem to have this problem.. What should I change to make it work?
Try replacing "\n" with "\r\n". That will likely do the trick if the script isn't working.
Bas
Netherlands #9: March 3, 2005
Thanks for the quick reaction.. Sorry for my slow reaction..

But it doesn't work.. Something else I could try to make the links work when they are the first (and only thing) on a line?
The \r and \n stuff should match new lines and whitespace in VBScript. Let me do a little testing though and I'll see if I can work out a definitive solution.
Bas
Netherlands #11: March 5, 2005
great thanks :)

I'll check to see if you found something.. :) Thanks for the effort!
Bruno
Brazil #12: August 26, 2005
Hi ILJD.
Your script just saved my life! GREAT JOB!

About the "." bug, I've inserted at the end of the first function, the following script.

replace(strText,".""",""".")

It's not perfect, but at leat lets the href works.

Tks
Bruno
Brian Hanifin
United States #13: February 22, 2006
Here's a more complete version of Bruno's solution:

' Remove unwanted punctuation at the end of the links (both the HREF and the text inside the anchor tag).
DIM UNWANTED_PUNCTUATION
UNWANTED_PUNCTUATION = "\?<\[\].,!"
strText = ereg_replace(strText, "[" & UNWANTED_PUNCTUATION & "]+""", """")
strText = ereg_replace(strText, "([" & UNWANTED_PUNCTUATION & "]+)</a>", "</a>$1")

This has worked on all of the text I have tested so far.
Mike
United States #14: April 3, 2006
The script works great!, thank you. One issue I've found is that URLS after char returns don't get parsed.

For example:
--- start example ---
www.yahoo.com is a great search portal.

www.google.com is great search engine.
--- end example ---

www.yahoo.com gets parsed, but www.google.com does not.

Any suggestions?
Anyone know what languages this solution will work for?
RADCOM
United Kingdom #16: June 4, 2007
complete code newbie :( I think this is exactly what i need but how do I make it work on a text list?

 

Post Your Comment

 
Only the name and comment fields are required.
 

Live Comment Preview

 United States #17: 1 minute ago

Web Design, Development and Marketing