Converting Strings To Hyperlinks

VB.Net Code:


Protected Function MakeLink() As String

Dim txt As String = divTxt.Text

Dim regx As New Regex("www.([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)

Dim mactches As MatchCollection = regx.Matches(txt)

For Each match As Match In mactches

txt = txt.Replace(match.Value, "<a href='http://" & match.Value & "' class='crm'>" & match.Value & "</a>")

Next

Return txt

End Function

Sub btnFind_Click(s As Object, e As EventArgs)

divLinks.InnerHtml = MakeLink()

End Sub

HTML:

Replace "litName" and "divName" with appropriate control names in your code.

<asp:Literal id="litName" runat="server" >Canine Zinc-responsive Dermatosis www.gopetsamerica.com/dog-health/zinc_responsive_dermatosis.aspx General facts about zinc-responsive dermatosis, a type of skin disease; breeds at risk; signs, diagnosis, and management. Bacitracin Zinc www.gopetsamerica.com/drugs/animal/bacitracin-zinc.aspx >h;/<asp:Literal>

<div id="divName" runat="server" ></div>

Click "RUN CODE" button to convert strings that contain "www." into hyperlinks:

Canine Zinc-responsive Dermatosis www.gopetsamerica.com/dog-health/zinc_responsive_dermatosis.aspx General facts about zinc-responsive dermatosis, a type of skin disease; breeds at risk; signs, diagnosis, and management. Bacitracin Zinc www.gopetsamerica.com/drugs/animal/bacitracin-zinc.aspx