/ Published in: C#
Will return a string that has any matched URLs wrapped in `<a>` tags.
Example: `"This is a link to http://foobar.com . Please visit !"`
Becomes: `"This is a link to <a href='http://foobar.com'>http://foobar.com</a> . Please visit!"`
Note: Opens links in new window.
Credit for regex:
[Faraz Shah Khan](http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx "Credit")
Example: `"This is a link to http://foobar.com . Please visit !"`
Becomes: `"This is a link to <a href='http://foobar.com'>http://foobar.com</a> . Please visit!"`
Note: Opens links in new window.
Credit for regex:
[Faraz Shah Khan](http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx "Credit")
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public static string FormatUrls(string input) { string output = input; Regex regx = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase); MatchCollection mactches = regx.Matches(output); foreach (Match match in mactches) { output = output.Replace(match.Value, "<a href='" + match.Value + "' target='blank'>" + match.Value + "</a>"); } return output; }