Link to home
Start Free TrialLog in
Avatar of phuctran
phuctran

asked on

regular expression in VBScript

Hi,

I have a string with some links in there
for example: str1 = " this is link1, link2 and link3"

I can use regular expression to find the links then make the Matches Object. Then use
  For Each strMatch In strMatches
    'with strMatch.Value and strMatch.Index
    'to replace link1 to alink1, link2 to alink2,
    'and link3 to alink3.
  Next
The problem is if link1=link2 then at the end I will have aalink1, aalink2, and alink3.  (notice double a's)
That is because, the first replace replaces link1 to alink1, then the second replace (for link2) will replace link1 in alink1 to alink1 -> then result is aalink1.

Does anyone know how to fix that problem?

Thanks in advance.


Avatar of knightEknight
knightEknight
Flag of United States of America image

a quick fix would be to run another expression after the first one that replaces "aalink" with "alink"
or, in the first expression, instead of scanning for "link" , scan for " link"  ( \slink )
Don't do this with For Each, loop through the string one time only.  This might be different from what you are looking for, but you get the idea;

<%
sString = "this is link1, link2 and link3"
Response.Write "Search string:" &sString

Set RegX = NEW RegExp
SearchPattern = "link"
RegX.Pattern = SearchPattern
RegX.Global = True
ReplacedText = RegX.Replace(sString, "a"&RegX.Pattern)

Response.Write "<p>New String: "&ReplacedText
%>
Avatar of phuctran
phuctran

ASKER

Sorry, I am not clear on my question.

The word is not "link".

for example:

this is www.yahoo.com, www.excite.com, and www.yahoo.com

I want to create http links for that string, so when I display it on browsers, I can click on that to go to the site.
just like this page, after I type www.yahoo.com then submit, there is a link for that.
try this function
----------------------
<%
Function replacelinks(textin)
Dim intLinkStartPos
Dim intLinkEndPos
Dim textout
Dim intLinkLenInc
Dim strLink
intLinkStartPos = InStr(textin, "http:")
If intLinkStartPos = 0 Then intLinkStartPos = InStr(textin, "https:")
If intLinkStartPos = 0 Then
    intLinkStartPos = InStr(textin, "www.")
Else
    If InStr(textin, "www.") <> 0 Then
        If intLinkStartPos>InStr(textin, "www.") Then
            intLinkStartPos = InStr(textin, "www.")
        End If
    End If
End If
While intLinkStartPos > 0
   
    intLinkEndPos = InStr(intLinkStartPos, textin, " ")
    If intLinkEndPos < intLinkStartPos Then
        intLinkEndPos = Len(textin) + 1
    End If
    strLink = Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos)
    If InStr(strLink, ",") > 0 Then
        strLink = Left(strLink, InStr(strLink, ",") - 1) & Mid(strLink, InStr(strLink, ",") + 1)
    End If
    intLinkLenInc = 16 + Len(strLink)
    If Left(Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos), 4) = "http" Then
        textin = Left(textin, intLinkStartPos - 1) & " <a href=" & Chr(34) & strLink & Chr(34) & ">" & Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos) & "</a>" & Mid(textin, intLinkEndPos )
    Else
        intLinkLenInc = intLinkLenInc + 7
        textin = Left(textin, intLinkStartPos - 1) & " <a href=" & Chr(34) & "http://" & strLink & Chr(34) & "> " & Mid(textin, intLinkStartPos, intLinkEndPos - intLinkStartPos) & "</a>" & Mid(textin, intLinkEndPos )
    End If
     
         
    intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "http:")
    If intLinkStartPos = 0 Then intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc,textin, "https:")
    If intLinkStartPos = 0 Then
        intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "www.")
    Else
        If InStr(intLinkEndPos + intLinkLenInc, textin, "www.") <> 0 Then
            If intLinkStartPos > InStr(intLinkEndPos + intLinkLenInc, textin, "www.") Then
                intLinkStartPos = InStr(intLinkEndPos + intLinkLenInc, textin, "www.")
            End If
        End If
    End If
Wend
replacelinks = textin
end function
%>
you use the function like this

<%=replacelinks("this is www.yahoo.com, www.excite.com,  and www.yahoo.com")%>

or

<%=replacelinks("this is http://www.yahoo.com, www.excite.com, and https://www.mysecuresite.com")%>

Don't be daft, a Regular expression is the only way to do this, and this is why:

http://aspemporium.com/aspEmporium/src/vbs_srcview.asp?source=regexp_LinkURLs.asp

You'll see how easy it is!!! ;-)
ASKER CERTIFIED SOLUTION
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can delete the "b" match.
AlfaNoMore,

Please find a 200-points question "points for AlfaNoMore" and accept that so I can give you the credit.

Thank you.
Diid you not mean to accept my comment as an answer?  If not, just send a message to Community Support to ask for a change.
mgfranz,

I accept yours and AlfaNoMore's.

However, the pattern is not used for all cases.  For example, if you have

s = "this is http://www.yahoo.com and <img src='http://www.yahoo.com/images/thisimage.gif'>"  then the http://... in src="" will be changed to <a href=""> also.

The link AlfaNoMore gave me have the same problem.

I will work on that case.  You gave me the right direction (using $1, $2); therefore, the points are yours.

Thank you.

Ahh, I see... A simple edit to the RegExp would solve this.  I'm sure you can kludge it up.