0

Microsoft の AntiXSSLibrary 4.0 の Sanitizer.GetSafeHtmlFragment を使用すると、HTML フラグメントが次のように変更されることに気付きました。

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

に:

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

残念ながら、彼らの API では、この動作を無効にすることはできません。したがって、正規表現 (C#) を使用して、class="" 属性内で発生する "x_anything" などの文字列を修正して "anything" に置き換えたいと考えています。

これを行うための正規表現を手伝ってくれる人はいますか?

ありがとう

更新- これは私にとってはうまくいきました:

 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;
        }
4

2 に答える 2

0

これが投稿されてから1年以上が経過しましたが、最大3つのクラスインスタンスを削除するために使用できる正規表現を次に示します。よりクリーンな方法があると確信していますが、それで仕事は終わります。

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")
于 2012-11-17T09:34:10.723 に答える
0

C# の @(Verbatim シンボル) について 100% 確信があるわけではありませんが、これx_は any の内部で一致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,"");
}
于 2011-07-18T19:20:00.047 に答える