Grabbing the HTML TITLE Tag's Contents

This demo illustrates how to use regular expressions to grab the contents of a TITLE tag. The HTML page we're accessing has the following contents:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>An example HTML page</TITLE>
</HEAD>
<BODY>

Hello, world!<br>
I've only got one thing to say: <b>Foobar!</b>
<p>
OK, that's <i>it</i>.

</BODY>
</HTML>

The source code of the ASP on this page is as follows:

<%
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Dim objFile
Set objFile = objFSO.OpenTextFile(Server.MapPath("/demos/ExampleHTML.html"))

Dim strContents
strContents = objFile.ReadAll

objFile.Close
Set objFile = Nothing
Set objFSO = Nothing

Dim objRegExp
Set objRegExp = New RegExp

Dim strHTML, strMatch
strMatch = "<title>(.*?)<\/title>"

objRegExp.Pattern = strMatch
objRegExp.IgnoreCase = True
objRegExp.Global = True

Dim objMatches
Set objMatches = objRegExp.Execute(strContents)

If objMatches.Count > 0 then
  Response.Write "The Web page title is: " & _
                 Mid(objMatches(0).Value, 8, Len(objMatches(0).Value) - 16)
Else
  Response.Write "No TITLE tag found."
End If

Set objRegExp = Nothing
%>


The Resulting Output

The Web page title is: An example HTML pag


[Return to the Article] | [View the Bold Regular Expressions Demo]