I'm looking for a regular expression to use in Refex.Replace that allows me to append a url with link elements. The idea is this:
http://www.tenforce.com => <a target='new' href='http://www.tenforce.com'>http://www.tenforce.com</a>
However, the regex is not allowed to do this when the URL is part of a html element, like for an image tag for example. So if we have for example:
<img src="http://www.tenforce.com/logo.jpg" />
It should not be converted using the regex.
The original regex that we used was this one:
@"(http|ftp|https):((\/\/)|(\\\\))[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
But this encodes every url it can find into the an a-tag. I don't want it to encode urls when they are prepended with src=\"
So I tried adding [^(src=.)] to it, but this results in normal urls no longer beeing transformed. it doesn't transform the image tags either though.
The code looks like this:
/// <summary>
/// Extends the text with hyperlinks.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="workspaceId">The workspace id where the user is working in. Used when parsing the wiki links</param>
/// <returns></returns>
public static string ExtendWithHyperlinks(string value, int? workspaceId)
{
if (value == null) return null;
const string UrlPattern = @"[^(src=.)](http|ftp|https):((\/\/)|(\\\\))[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
const string FilePattern = @"(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\\w((\.*\w+)|( *\w+))*)+";
value = Regex.Replace(value, UrlPattern, "<a target='new' href='$0'>$0</a>").Replace(":\\\\", "://");
value = Regex.Replace(value, FilePattern, "<a target='new' href='file:///$0'>$0</a>");
value = TemplateParser.Parse(value, workspaceId, Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.Remove(0, 8))));
return value;
}