Sometimes we need a quick, reliable way to be able to strip certain HTML tags from a string. The Regular Expression engine (RegExp) built into ASP is ideal for this purpose.
Real World Example
We were building a newsletter system into our FindLocalVehicles.co.uk website. The website has daily motoring news as well as the vehicles for sale listings. We wanted to be able to include the latest featured article from the website in the weekly newsletter.
Now, there are sometimes images posted in these news articles, and if we included the images in our newsletter contents, they would be too big and "break" the layout.
The simplest option for this problem is to use RegExp to strip out the images from the articles before including them into the newsletter.
Here’s how you do it:
Setup a function to call
Function stripTags(HTMLstring)
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
‘.Pattern = "<[^>]+>" use for all html tags
.Pattern = "<img[^<>]+>" ‘use for images
.IgnoreCase = True
.Global = True
End WithstripTags = RegularExpressionObject.Replace(HTMLstring, "")
Set RegularExpressionObject = nothingEnd Function
Call it from the ASP page
stripTags("string")
In the above example, if you wanted to strip out all HTML tags and not just the img tags, un-comment out the ‘.Pattern = "<[^>]+>" use for all html tags bit and comment out the line below it.
Tagged : classic asp,regular expressions