2011-04-14 44 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\" />"); 
相關問題