要实现的效果:让一段文本中所有超链接都在新窗口中打开。有些<a>没有定义target属性,则要用正则加进去,如果有target属性,但属性值不是_blank,也要改为_blank。
解决方案 »
- 控件如何窗体居中?
- 如何可以在C#2005中连接SQL2005时使用WINDOWS的登录密码
- 请问:用了反射后是否能有混淆?
- FetionSdk问题
- 用批复日期>= #"+dt1.Value.ToShortDateString ()+"# and 批复日期<= #"+ dt2.Value.ToShortDateString () +"# 查询Access数据库,为什
- SQL转Oracle问题 谢谢了!
- 关于C#SerialPort控件设置与COMMTIMEOUTS 的问题!!!急!
- 两个小问题
- 如何在杂乱的背景上绘制字符串?
- 问题请教:微信小程序socket连接WSS,安卓和开发环境都正常,苹果端报"OSStatus"错误-9801
- 精度问题
- 关于System.Array.BinarySearch()方法很奇怪的问题
string str = "<a class=\"zzz\">this is one</a><br/>"
+ "<a class=\"zzz\" target=\"_self\">this is one</a><br/>"
+ "<a class=\"zzz\" target=\"_blank\">this is one</a><br/>";
string result = Regex.Replace(str, @"(?is)(?<=<a[^>]*)(target=['""\s]([^'""\s]+)['""\s])", delegate(Match m)
{
if (m.Groups[2].Value == "_blank")
return m.Value;
else
return "target=\"_blank\"";
});
result = Regex.Replace(result, @"(?is)(<a(?![^>]*?target=['""][^'""]*?['""]))", "$1 target=\"_blank\"");
Response.Write(result + "<br/>");
/*
<a target="_blank" class="zzz">this is one</a>
<a class="zzz" target="_blank">this is one</a>
<a class="zzz" target="_blank">this is one</a>
*/
+ "<a class=\"zzz\" target=\"_self\">this is one</a>\n"
+ "<a class=\"zzz\" target=\"_blank\">this is one</a>\n";
string result = Regex.Replace(str, @"(?is)(?<=<a(?:(?!\btarget\b)[^>])*)(?:\starget=(['""]?)((?:(?!\1).)*)\1(?=[^>]*>)|(?=>))", delegate(Match m)
{
if (m.Groups[2].Value == "_blank")
return m.Value;
else
return " target=\"_blank\"";
});
richTextBox2.Text = result;
/*------输出-------
<a class="zzz" target="_blank">this is one</a>
<a class="zzz" target="_blank">this is one</a>
<a class="zzz" target="_blank">this is one</a>
*/