首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复GetSafeHtmlFragment x_前缀的Regex

修复GetSafeHtmlFragment x_前缀的Regex
EN

Stack Overflow用户
提问于 2011-07-18 19:08:07
回答 2查看 682关注 0票数 0

在使用MicrosoftHTML4.0中的Sanitizer.GetSafeHtmlFragment时,我注意到它从以下几个方面更改了我的AntiXSSLibrary片段:

代码语言:javascript
复制
<pre class="brush: csharp">
</pre>

至:

代码语言:javascript
复制
<pre class="x_brush: x_csharp">
</pre>

可悲的是,他们的API不允许我们禁用这种行为。因此,我想使用正则表达式(C#)来修复和替换出现在class="“属性中的字符串,比如"x_anything”。

有人能帮我做RegEx吗?

谢谢

UPDATE --这对我有用:

代码语言:javascript
复制
 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;
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-18 19:20:00

Im对C# @(逐字符号)不是百分之百肯定,但我认为这应该与任何class=""中的x_匹配,并用空字符串替换它:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-11-17 09:34:10

这篇文章已经发布了一年多了,但是这里有一些正则表达式,您可以使用它来删除最多三个类实例。我相信有一个更清洁的方法,但它可以完成工作。

VB.Net代码:

代码语言:javascript
复制
Regex.Replace(myHtml, "(<\w+\b[^>]*?\b)(class="")x[_]([a-zA-Z]*)( )?(?:x[_])?([a-zA-Z]*)?( )?(?:x[_])?([^""]*"")", "$1$2$3$4$5$6$7")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6737997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档