I am currently displaying the content of a string inside a pre tag, but I have to elaborate a function that for each link into the string replace it with a link tag, I tried several string replace and regex methods, but no one worked.
string myString = "Bla bla bla bla http://www.site.com and bla bla http://site2.com blabla"
//Logic
string outputString = "Bla bla bla bla <a href="http://www.site.com" target="blank">http://www.site.com</a> and bla bla <a href="http://site2.com" target="blank">http://site2.com</a> blabla"
I have used the following code, but it does not work for every url:
string orderedString = item.Details.Replace("|", "\n" );
string orderedStringWithUrl = "";
System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection mactches = regx.Matches(orderedString);
foreach (System.Text.RegularExpressions.Match match in mactches)
{
orderedStringWithUrl = orderedString.Replace(match.Value, "<a href='" + match.Value + "' target='blank'>" + match.Value + "</a>");
}
Any suggestion?
Update: I have noticed that the URLs I have in the string are all without spaces and all starts with http or https. Is it an idea the one to put into a everything that starts with http or https up to (and not included) the first space? In that case how can I use the .replace to achieve this?
Tnx in advance.