Detect URL from Label value and convert it to link using RegularExpression in ASP.Net

RichardSa
 
on Jun 24, 2021 10:19 PM
Sample_361704.zip
928 Views

Hello forum,

How do I make asp:label to detect any link in a series of texts inside a label?

For example: if a user types messages and in the message the user types a Web link. I want that link to change color and be clickable.

I could type something like this:

"Hello people please I have good news this morning, check it out on www.goodnews.com, it's one you don't want to miss".

From the above message texts, www.goodnews.com is a link that will show in blue color and it can be clicked. While other texts will be black.

Is it that this is an automated detection that when the website is hosted (online) it will detect it automatically or is it done with coding?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 24, 2021 11:07 PM

Hi RichardSa,

Check this example. Now please take its reference and correct your code.

HTML

<asp:Label ID="lblData" Text="" runat="server" />
<style type="text/css">
    a:link {
        text-decoration: none;
        cursor: pointer;
        color: blue;
    }
</style>

Namespaces

C#

using System.Text.RegularExpressions;

VB.Net

Imports System.Text.RegularExpressions

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    lblData.Text = "Hello people please I have good news this morning, check it out on www.goodnews.com, it's one you don't want to miss";
    lblData.Text = ConvertTextUrlToLink(lblData.Text);
}

public string ConvertTextUrlToLink(string rawString)
{
    Regex linkParser = new Regex(@"\b(?:http://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    foreach (Match m in linkParser.Matches(rawString))
    {
        if (m.Value.IndexOf("http") != -1 || m.Value.IndexOf("https") != -1)
        {
            rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = '{0}'>{0}</a>", m.Value));
        }
        else
        {
            rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = 'http://{0}'>{0}</a>", m.Value));
        }
    }

    return rawString;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    lblData.Text = "Hello people please I have good news this morning, check it out on www.goodnews.com, it's one you don't want to miss"
    lblData.Text = ConvertTextUrlToLink(lblData.Text)
End Sub

Public Function ConvertTextUrlToLink(ByVal rawString As String) As String
    Dim linkParser As Regex = New Regex("\b(?:http://|www\.)\S+\b", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
    For Each m As Match In linkParser.Matches(rawString)
        If m.Value.IndexOf("http") <> -1 OrElse m.Value.IndexOf("https") <> -1 Then
            rawString = rawString.Replace(m.Value, String.Format("<a target='_blank' href = '{0}'>{0}</a>", m.Value))
        Else
            rawString = rawString.Replace(m.Value, String.Format("<a target='_blank' href = 'http://{0}'>{0}</a>", m.Value))
        End If
    Next

    Return rawString
End Function

Screenshot