2011-04-14 45 views
1

如果有人在我网站的产品页面上,我想覆盖元描述标记。C#替换这些标记之间的所有内容

<meta name="description" content=" " /> 

我使用的CMS已在此代码预渲染它的页面模板的方法:

this.ltlTags.Text = this.HeaderTags; 

这种填充与meta标签,CSS标签,脚本代码和所有的头。

我想说的是这样的:

this.ltlTags.Text = this.HeaderTags.Replace(
    "everything inside the content attribute of the meta tag", "with this text"); 

在C#中有什么办法,我可以做到这一点?

+0

我不确定你提供了足够的信息来回答这个问题。 'this.HeaderTags'是什么?如果它包含多个标签,它们是如何分隔的等等? – 2011-04-14 14:57:29

回答

1

像这样的东西应该工作(未经测试):

this.ltlTags.Text = Regex.Replace(this.HeaderTags, 
    "content=\"[^\"]*\"", "content=\"" + yourStuff + "\""); 

它基本上取代content="<anything>"content="<yourStuff>"

+0

真棒!你已经保存了我的培根。非常感谢! – phil 2011-04-14 15:02:45

0

你能替换整个标签吗?我的意思是

this.ltlTags.Text = this.HeaderTags.Replace("<meta name=\"description\" content=\" \" />", 
          "<meta name=\"description\" content=\"new text here\" />"); 
相关问题