文内广告文字链接替换

需要把正文内容里面的文字替换,但是标记内的不替换, 可以使用正则来实现.

        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();
        }


        
static  string DoReplace(Match m)
        
{
            
if (m.Groups["content"].Success) 
                
return m.Value.Replace("电脑","<a href=\"computer.aspx\">电脑</a>");
            
else 
                
return m.Value;
        }


posted on 2008-06-12 11:44  沙加  阅读(642)  评论(2编辑  收藏  举报

导航