2011-05-19 36 views
2

我想要一個正則表達式來從字符串中刪除html標記和&等等。我得到的正則表達式是移除html標籤,但不提及其他人。我使用的.Net 4string刪除htmls

感謝

CODE:

 String result = Regex.Replace(blogText, @"<[^>]*>", String.Empty); 
+1

繼續之前,看看這裏:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Zruty 2011-05-19 15:58:53

+2

呃哦...... ... – 2011-05-19 15:59:08

+0

正則表達式和HTML從來都不是一個好的組合。看看@ http://stackoverflow.com/questions/5496704/strip-html-and-css-in-c – 2011-05-19 16:00:07

回答

0

要建立在您已創建的內容上,您可以將其更改爲以下內容:

String result = Regex.Replace(blogText, @"<[^>]*>|&\w+", String.Empty); 

這意味着...

    爲您定義
  1. 要麼匹配標籤...
  2. ...或匹配後跟一個&至少一個字字符\w - 多達可能。

這兩個都不能在所有討厭的情況下工作,但通常情況下它確實如此。