2011-07-18 25 views
0

當從微軟的AntiXSSLibrary 4.0使用Sanitizer.GetSafeHtmlFragment,我注意到它改變了我的HTML片段:正則表達式來解決GetSafeHtmlFragment X_前綴

<pre class="brush: csharp"> 
</pre> 

到:

<pre class="x_brush: x_csharp"> 
</pre> 

可悲的是他們的API不允許我們禁用這種行爲。因此,我希望使用正則表達式(C#)來修復和替換類「x_anything」到「任何」的字符串,這些字符串出現在class =「」屬性中。

任何人都可以用RegEx幫助我做到這一點嗎?

感謝

UPDATE - 這個工作對我來說:

private string FixGetSafeHtmlFragment(string html) 
     { 
      string input = html; 
      Match match = Regex.Match(input, "class=\"(x_).+\"", RegexOptions.IgnoreCase); 

      if (match.Success) 
      { 
       string key = match.Groups[1].Value; 
       return input.Replace(key, ""); 
      } 
      return html; 
     } 

回答

0

我不是100%地肯定了C#@(逐字符號),但我認爲這應該匹配x_任何class=""的內將其替換爲空字符串:

string input = 'class="x_something"'; 
Match match = Regex.Match(input, @'class="(x_).+"', 
    RegexOptions.IgnoreCase); 

if (match.Success) 
{ 
    string key = match.Groups[1].Value; 
    string v = input.Replace(key,""); 
} 
+0

感謝,公民康涅狄格州。 – Anon2321

0

已經過了一年多了,因爲已經發布了bu這裏有一些你可以使用的正則表達式,最多可以刪除三個類實例。我確信有一個更清潔的方式,但它完成了工作。

VB.Net代碼:

Regex.Replace(myHtml, "(<\w+\b[^>]*?\b)(class="")x[_]([a-zA-Z]*)()?(?:x[_])?([a-zA-Z]*)?()?(?:x[_])?([^""]*"")", "$1$2$3$4$5$6$7")