修复 GetSafeHtmlFragment x_ 前缀的正则表达式
当使用 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;
}
When using Sanitizer.GetSafeHtmlFragment from Microsoft's AntiXSSLibrary 4.0, I noticed it changes my HTML fragment from:
<pre class="brush: csharp">
</pre>
to:
<pre class="x_brush: x_csharp">
</pre>
Sadly their API doesn't allow us to disable this behavior. Therefore I'd like to use a regular expression (C#) to fix and replace strings like "x_anything" to "anything", that occur inside a class="" attribute.
Can anyone help me with the RegEx to do this?
Thanks
UPDATE - this worked for me:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 C# @(逐字符号) 不是 100% 确定,但我认为这应该与任何
class=""
内部的x_
匹配,并将其替换为空字符串:Im not 100% sure about the C# @(Verbatim symbol) but I think this should match
x_
inside of anyclass=""
and replace it with an empty string:这篇文章发布已经一年多了,但您可以使用以下一些正则表达式来删除最多三个类实例。我确信有一种更干净的方法,但它可以完成工作。
VB.Net代码:
It's been over a year since this has been posted but here's some regex you can use that will remove up to three class instances. I'm sure there's a cleaner way but it gets the job done.
VB.Net Code: