文内广告文字链接替换
需要把正文内容里面的文字替换,但是标记内的不替换, 可以使用正则来实现.
static void Main(string[] args)
{
string content = "<div>电脑是个好东西,我们大家都喜欢<img src=\"3w.jpg\" alt=\"电脑\"/>你喜欢电脑么? <a href=\"index.htm\" title=\"电脑\"></a>哈哈,就是这个东西。</div>";
string newContent = Regex.Replace(content, "([^\"]+?)(?<content>电脑)([^\"].+?)", new MatchEvaluator(DoReplace));
Console.Write(newContent);
Console.ReadLine();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
static string DoReplace(Match m)
{
if (m.Groups["content"].Success)
return m.Value.Replace("电脑","<a href=\"computer.aspx\">电脑</a>");
else
return m.Value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)