RegExp and Replacing Submatches

G

Guest

I have a value, retrieved from a recordset, that contains any number of
hyperlinks to a number. For example:

<a href="123456">Hi</a> <a href='334455'>Hola</a>

What I need to do is parse the string, identify any such valid
hyperlinks, look up each of those numbers in a database, and return a
string that looks something like this:

<a href="/somepage.asp">Hi</a> <a href='/anotherpage.asp'>Hola</a>

Here's the VBScript/ASP code I have written so far:
========================================================
Sub ReplaceSubmatches(sInputHTML)
Dim sReturnValueHTML, iCount, oRegExp, oMatches, oMatch
Dim sHrefValue, sHrefNewValue

sReturnValueHTML = sInputHTML

iCount = 0
Set oRegExp = New RegExp
oRegExp.Pattern = "href[\s]?=[\s\""\']+(.*?)[\""\']+"
oRegExp.Global = True
Set oMatches = oRegExp.Execute(sInputHTML)

If (oMatches.Count > 0) Then
For Each oMatch In oMatches
If (oMatch.Submatches.Count = 1) Then
iCount = iCount + 1
sHrefValue = Trim(oMatch.Submatches(0) & "")
sHrefNewValue = "somevalue" & iCount

If (StrComp(sHrefValue, sHrefNewValue, 1) <> 0) Then
Response.Write("Replace " & sHrefValue & " with " &
sHrefNewValue & "<br>")
'// Do something to [sReturnValueHTML]
End If
End If
Next
End If

Set oMatches = Nothing
Set oRegExp = Nothing

Response.Write("<br>" & vbNewLine)
Response.Write("Original = <u>" & Server.HTMLEncode(sInputHTML) &
"</u><br>")
Response.Write("New = <u>" & Server.HTMLEncode(sReturnValueHTML) &
"</u><br>")
If (StrComp(sInputHTML, sReturnValueHTML, 0) = 0) Then
Response.Write("Values are identical.<br>")
End Sub

Call ReplaceSubmatches("<a href=""123456"">Hi</a> <a
href='334455'>Hola</a>")
========================================================

The main reason I use a regular expression, instead of a straight
Replace() statement, is that the href tags may use double-quotes
(href="..") or single-quotes (href='..') to define a value. What do I
need to do to the above code ( '// Do something to [sReturnValueHTML] )
to replace each href value?



-= Tek Boy =-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RegExp 2
RegExp: Replacing with submatch 3
Help with code 0
Replacing text, fails totally 1
Help with Visual Lightbox: Scripts 2
Visit Counter II 0
error '80020009' help 2
Issue with textbox script? 0

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top