2013-09-21 48 views
0

我需要在我的textarea中允許特定的HTML標籤,並忽略所有其他標籤。 例如< b>將被允許,但是如果< h1>在那裏,我希望標籤替換爲其內部文本。只允許ASP.Net中的HTML標籤的一個子集textarea

< B>一些文本</B>將是:一些文字 和< H1>一些文本</h1>將是:一些文字

我也想抽出形式的所有屬性都允許標籤。

這是可能的正則表達式?

更新:我試過這種負面看法<(?!b|br|p|i|u|sup|sub|br|ul|ol|li|a).*(\s[^>]*)?>,但它匹配整個文檔。如果有人能夠解決這個負面看法,那將是非常好的。

+0

HTML不與regex..use一個HTML解析parser..also讓你的問題specific.right現在你已經問了很多,我們將不得不關閉這個答案,除非你編輯它,使它更具體 – Anirudha

+0

它幾乎不能稱爲'解析'。不需要配對。我只需要檢測單個標籤。 – Vahid

+0

如何解析(赦免)檢測:'

一些文字

'? –

回答

0

我找到了自己的路。

如果有人在將來需要這個,這就是我所做的。

 // remove HTML comments 
     Regex regex = new Regex(@"<!--[^-]*-->", RegexOptions.IgnoreCase); 
     input = regex.Replace(input, ""); 
     // remove unsupported tags 
     regex = new Regex(@"</?((?!p|i|u|sup|sub|br|ol|li|a|b|/)|((p|i|sup|sub|br|ul|ol|li|a|b)[^<>\s]+))[^<>]*?>", RegexOptions.IgnoreCase); 
     input = regex.Replace(input, ""); 
     // remove attribiutes 
     regex = new Regex(@"(<[^\s]+)(\s[^<>]*)(>)", RegexOptions.IgnoreCase); 
     input = regex.Replace(input, m => m.Groups[1].Value + m.Groups[3].Value); 
     return input;