62,268
社区成员
发帖
与我相关
我的任务
分享
void Main()
{
string[] domains = { "aa.com", "bb.com", "tmall.com" };
string domainReg = string.Join("|",domains);
string pattern=@"(?i)(?<!<a\s+href=(['""])?(?:https?://)?)(?:https?://)?www\.(\w+\.)?(?:{0})[^'""<\s]*";
string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
string source2 = Regex.Replace(source, string.Format(pattern,domainReg),"<a href='http://$0' target='_blank'>$0</a>");
Response.Write("加链接前:" + source);
Response.Write("<br />");
Response.Write("加链接后:" + source2);
/*
加链接前:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=
加链接后:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 <a href='http://www.aaaa.aa.com/xxx.aspx' target='_blank'>www.aaaa.aa.com/xxx.aspx</a> 123123<br />123123123<a href='http://www.aa.com' target='_blank'>www.aa.com</a><br /><a href='http://http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=' target='_blank'>http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=</a>
*/
protected void Page_Load(object sender, EventArgs e)
{
string domainReg = string.Empty;
string[] domains = { "aa.com", "bb.com", "tmall.com" };
foreach (string domain in domains)
{
domainReg += domain + "|";
}
if (!string.IsNullOrEmpty(domainReg))
{
domainReg = domainReg.TrimEnd('|');
}
string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
string source2 = Regex.Replace(source, string.Format(@"(?is)(?<!<a\b\s+href=(['""]))((?:http(s)?://)|(?:www\.))(\w+\.)?({0})([^'""<\s]+)?", domainReg), "<a href='http://$0' target='_blank'>$0</a>");
Response.Write("加链接前:" + source);
Response.Write("<br />");
Response.Write("加链接后:" + source2);
}
http://download.csdn.net/detail/q107770540/2844387
string source = "www.a.aa.com/xxx.htm<a href='http://www.a.aa.com/xxx.htm'>xxx</a>www.b.bb.com/xxx.htm";
source = Regex.Replace(source, @"(?i)(?<!<a\s+href=(['""])?.*?)(?:https?://)?www\.a\.(?:aa|bb)\.com[^'""\s]+", "<a href='$0' target='_blank'>$0</a>");
Response.Write(source);