Classic ASP Regular Expression Autolink Function
Posted on 16.05.09 01:23:29 by Ariel G. Saputra in Classic ASP, Tutorials
During my session in creating Simple Twitter API for Classic ASP (VBScript), I need a function to search valid URL format in body text and convert them into click able link (autolink). Fortunately as in other programming languages, Classic ASP (VBScript) also has support REGULAR EXPRESSION even with limited methods & Properties. In this case we only need the Regexp.Replace method, below is the detail function.
Classic ASP Regexp Autolink Function
'********************************************* '* @TITLE: Regexp Autolink Function '* @PACKAGE: Simple Classic ASP Twitter API '* @AUTHOR: Ariel G. Saputra <webmaster@asp.web.id> '* @DESCRIPTION: function to search and replace url in a text into clickable url '* @DATE: May 15 2009 '********************************************* function aspTwitterAutolink(strHtml) Dim objRegex,strReturn set objRegex = new regexp objRegex.Pattern = "(\b(?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$]))" objRegex.IgnoreCase = true objRegex.Global = true strReturn = objRegex.Replace(strHtml, "<a href=""$1"" rel = ""nofollow"" target = ""_blank"">$1</a>") set objRegex = nothing aspTwitterAutolink = strReturn end function
Sample of Usage
Dim strHtml strHtml = "Just a test HTML text http://www.asp.web.id with valid url, https://php.web.id, regexp turn these links on!" Response.Write aspTwitterAutolink(strHtml)
Happy ASP programming,
:G cwit dlim yah!
Author: Ariel G. Saputra
6 Comments For This Post
1 Trackbacks For This Post
-
17.05.09 20:44:25 at 20:44
Autolink with classic asp & regular expression Says:
[...] implementation of regular expression in Classic ASP script to create link from text (autolink), this is useful when you need to turn a bunch of text to be a clickable link when it is in a valid [...]



roei
22/06/09 20:05
it’s not working
SzR
22/06/09 21:13
autolinkfunction is not working.
Ariel G. Saputra
30/06/09 07:21
@roei + SzR
yeah sorry all the escape chars was stripped out in the pattern, it should be working now.
thanks for reporting
Bauke
26/01/10 00:06
Great!
a) One of the first results of my query in Google
a) Works like a charm!
Thanks a lot
jannerman
16/03/10 19:59
great bit of code, the only way it could be better was if it was able to interpret http://www.google.co.uk and pass it as http://www.google.co.uk rather than http://www.thehostwebsite.co.uk/www.google.co.uk but it’ll do for now!
Pablo
01/04/10 20:52
Very cool…awesome use of Regex!
Here’s a more complete function which isn’t as pretty, but will handle many more variations of links:
‘::Auto-Link Function
Function AutoLink(str,target)
‘split into multiple ‘words’
If IsNull(str) OR str = “” Then Exit Function
str = Split(Trim(str))
‘loop through and process each ‘word’
For wCnt = 0 To UBound(str)
wStr = str(wCnt)
‘check for url wString
If (Left(LCase(wStr),4) = “http” AND Len(wStr) > 11 AND InStr(wStr,”://”) > 0) _
OR (Left(LCase(wStr),4) = “www.” AND Len(wStr) > 11 AND InStrRev(wStr,”.”) > 4) Then
arrChar = “! 0 Then
lwStr = Right(wStr,Len(wStr)-locChar+1)
wStr = Left(wStr,locChar-1)
Exit For
End If
Next
If InStr(LCase(wStr),”http://”) = 0 Then pwStr = “http://”
wStr = “” & wStr & “” & lwStr
‘check for email wString
ElseIf (InStr(wStr,”@”) > 0 AND len(wStr) > 4 _
AND InStr(wStr,”@”) = InStrRev(wStr,”@”) _
AND InStr(wStr,”href=mailto:”) <= 0) Then
wStr = "” & wStr & “”
End If
‘save newly formatted ‘word’
str(wCnt) = wStr
Next
‘combine and return completely formatted string
AutoLink = Join(str)
End Function