[ACCEPTED]-Easiest way to convert a URL to a hyperlink in a C# string?-hyperlink

Accepted answer
Score: 23

Regular expressions are probably your friend 2 for this kind of task:

Regex r = new Regex(@"(https?://[^\s]+)");
myString = r.Replace(myString, "<a href=\"$1\">$1</a>");

The regular expression 1 for matching URLs might need a bit of work.

Score: 7

I did this exact same thing with jquery consuming the JSON API here is 1 the linkify function:

String.prototype.linkify = function() {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
        return m.link(m);
Score: 5

This is actually an ugly problem. URLs 13 can contain (and end with) punctuation, so 12 it can be difficult to determine where a 11 URL actually ends, when it's embedded in 10 normal text. For example:


is a valid URL, but 9 it could just as easily be the end of a 8 sentence:

I buy all my witty T-shirts from http://example.com/.

You can't simply parse until a 7 space is found, because then you'll keep 6 the period as part of the URL. You also 5 can't simply parse until a period or a space 4 is found, because periods are extremely 3 common in URLs.

Yes, regex is your friend 2 here, but constructing the appropriate regex 1 is the hard part.

Check out this as well: Expanding URLs with Regex in .NET.

Score: 1

/cheer for RedWolves

from: this.replace(/[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/, function(m){...

see: /[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/

There's 19 the code for the addresses "anyprotocol"://"anysubdomain/domain"."anydomainextension 18 and address",

and it's a perfect example 17 for other uses of string manipulation. you 16 can slice and dice at will with .replace 15 and insert proper "a href"s where needed.

I 14 used jQuery to change the attributes of 13 these links to "target=_blank" easily in 12 my content-loading logic even though the 11 .link method doesn't let you customize them.

I 10 personally love tacking on a custom method 9 to the string object for on the fly string-filtering 8 (the String.prototype.linkify declaration), but 7 I'm not sure how that would play out in 6 a large-scale environment where you'd have 5 to organize 10+ custom linkify-like functions. I 4 think you'd definitely have to do something 3 else with your code structure at that point.

Maybe 2 a vet will stumble along here and enlighten 1 us.

Score: 1

You can add some more control on this by 3 using MatchEvaluator delegate function with 2 regular expression: suppose i have this 1 string:

find more on http://www.stackoverflow.com 

now try this code

private void ModifyString()
    string input = "find more on http://www.authorcode.com ";
                Regex regx = new Regex(@"\b((http|https|ftp|mailto)://)?(www.)+[\w-]+(/[\w- ./?%&=]*)?");
                string result = regx.Replace(input, new MatchEvaluator(ReplaceURl));

static string ReplaceURl(Match m)
    string x = m.ToString();
    x = "< a href=\"" + x + "\">" + x + "</a>";
    return x;

More Related questions