-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkExtension.cs
More file actions
31 lines (25 loc) · 1.02 KB
/
LinkExtension.cs
File metadata and controls
31 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Extensions
{
public static class LinkExtension
{
public static string Linkify(this string text, string target = "_self")
{
Regex domainRegex = new Regex(@"(((?<scheme>http(s)?):\/\/)?([\w-]+?\.\w+)+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\,]*)?)", RegexOptions.Compiled | RegexOptions.Multiline);
return domainRegex.Replace(
text,
match => {
var link = match.ToString();
var scheme = match.Groups["scheme"].Value == "https" ? Uri.UriSchemeHttps : Uri.UriSchemeHttp;
var url = new UriBuilder(link) { Scheme = scheme }.Uri.ToString();
return string.Format(@"<a href=""{0}"" target=""{1}"">{2}</a>", url, target, link);
}
);
}
}
}