我想剪切所有 url 之类的(http://...。)并将它们替换为锚点<a></a>
,但我的要求:不要触摸锚点和页面定义(文档类型),例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
所以我需要找到带有url的纯文本......
我正在尝试覆盖我在页面内的渲染,并制作了 BrowserAdapter:
<browser refID="default">
<controlAdapters>
<adapter controlType="System.Web.Mvc.ViewPage"
adapterType="Facad.Adapters.AnchorAdapter" />
</controlAdapters>
</browser>
它看起来像这样:
public class AnchorAdapter : PageAdapter
{
protected override void Render(HtmlTextWriter writer)
{
/* Get page output into string */
var sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
var htw = new HtmlTextWriter(tw);
// Render into my writer
base.Render(htw);
string page = sb.ToString();
//regular expression
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
//get the first match
Match match = regx.Match(page);
//loop through matches
while (match.Success)
{
//output the match info
System.Web.HttpContext.Current.Response.Write("<p>url match: " + match.Groups[0].Value+"</p>");
//get next match
match = match.NextMatch();
}
writer.Write(page);
}
}