【发布时间】:2013-12-13 20:36:20
【问题描述】:
我正在集成服务器端 Markdown 编译库 MarkdownSharp。我有这个工作,但现在我需要清理生成的 html。
我查看了 Stack Exchange Data Explorer 源代码以了解他们如何清理 html,并看到他们使用以下正则表达式在转换后清理图像:
private static readonly Regex _whitelist_img =
new Regex(
@"
^<img\s
src=""https?://[-a-z0-9+&@#/%?=~_|!:,.;\(\)]+""
(\swidth=""\d{1,3}"")?
(\sheight=""\d{1,3}"")?
(\salt=""[^""<>]*"")?
(\stitle=""[^""<>]*"")?
\s?/?>$",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled |
RegexOptions.IgnorePatternWhitespace);
我一直在努力为 whitelist_iframe 编写一个类似的正则表达式 - 确保 iframe 包含来自 youtube 或 vimeo 的链接。以下链接是我想嵌入的示例:
<iframe width="560" height="315" src="//www.youtube.com/embed/IZ_ScEebDOM?rel=0" frameborder="0" allowfullscreen></iframe>
<iframe src="//player.vimeo.com/video/80825843?title=0&byline=0&portrait=0" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
所以我认为以上需要修改为:
-
//帐户,而不是 http 或 https -
</iframe>结束标记的帐户 -
//www.youtube.com或//player.vimeo.com的帐户需要在src标记的开头。
我正在将其作为我的第一个正则表达式进行处理...对此的任何帮助将不胜感激。
请注意,我不打算在这里用更好的整体方法引入额外的库或复杂性,我只是想用正则表达式来增强已经工作的代码。
【问题讨论】:
-
好的...所以你不想讨论这可能不是一个好主意。没关系。您至少可以向我们展示您到目前为止所做的尝试吗?现在这只是一个“给我代码”类型的问题。
标签: regex iframe sanitization markdownsharp