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 =-
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 =-