我正在集成服务器端 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
我正在将其作为我的第一个正则表达式进行处理……对此的任何帮助将不胜感激。
请注意,我不想在这里用更好的整体方法引入额外的库或复杂性,我只是想用正则表达式来增加已经工作的代码。