编码 HTML 的正则表达式
我想创建一个正则表达式,它将匹配仅包含 href 属性的开始 标记:
<a href="doesntmatter.com">
它应该与上面的内容匹配,但在添加其他属性时不匹配:
<a href="doesntmatter.com" onmouseover="alert('Do something evil with Javascript')">
通常情况下是这样很简单,但是 HTML 是经过编码的。 因此,对上述两者进行编码,我需要正则表达式来匹配此:
<a href="doesntmatter.com" >
但不匹配此:
<a href="doesntmatter.com" onmouseover="alert('do something evil with javascript.')" >
假设所有编码的 HTML 都是“有效”(没有奇怪的格式错误的 XSS 欺骗)并假设我们不需要遵循任何 HTML 清理最佳实践。 我只需要最简单的正则表达式来匹配上面的 A) 但不匹配 B)。
谢谢!
I'd like to create a regex that will match an opening <a>
tag containing an href attribute only:
<a href="doesntmatter.com">
It should match the above, but not match when other attributes are added:
<a href="doesntmatter.com" onmouseover="alert('Do something evil with Javascript')">
Normally that would be pretty easy, but the HTML is encoded. So encoding both of the above, I need the regex to match this:
<a href="doesntmatter.com" >
But not match this:
<a href="doesntmatter.com" onmouseover="alert('do something evil with javascript.')" >
Assume all encoded HTML is "valid" (no weird malformed XSS trickery) and assume that we don't need to follow any HTML sanitization best practices. I just need the simplest regex that will match A) above but not B).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我首先想到的正则表达式是
//
; 惰性表达式 (.*?
) 可用于匹配引号之间的字符串。 然而,正如评论中所指出的,因为正则表达式是由 > 锚定的,所以它也会匹配无效标签,因为仍然会匹配。为了解决这个问题,您可以使用原子分组。 原子分组告诉正则表达式引擎,“一旦找到该组的匹配项,就接受它”——这将解决正则表达式在找不到 > 后返回并匹配第二个字符串的问题。 a href 的结尾。 具有原子组的正则表达式如下所示:
当用 HTML 实体替换字符时,它将如下所示:
The initial regular expression that comes to mind is
/<a href=".*?">/
; a lazy expression (.*?
) can be used to match the string between the quotes. However, as pointed out in the comments, because the regular expression is anchored by a >, it'll match the invalid tag as well, because a match is still made.In order to get around this problem, you can use atomic grouping. Atomic grouping tells the regular expression engine, "once you have found a match for this group, accept it" -- this will solve the problem of the regex going back and matching the second string after not finding a > a the end of the href. The regular expression with an atomic group would look like:
Which would look like the following when replacing the characters with their HTML entities:
嘿! 我最近不得不做类似的事情。 我建议先解码 html,然后尝试获取您想要的信息。 这是我的 C# 解决方案:
希望有帮助!
Hey! I had to do a similar thing recently. I recommend decoding the html first then attempt to grab the info you want. Here's my solution in C#:
I hope that helps!
我不明白匹配的一个与另一个有什么不同? 您只需精确查找刚刚编写的内容,将
doesntmatter.com
部分作为您捕获的部分。 我猜匹配"
(不是"
?)之前的任何内容都会出现问题,但您可以在正则表达式中这样做:本质上意味着:
"""
,则匹配失败完整的正则表达式将是:
这比使用更高效非贪婪的表达式。
感谢 Daniel Vandersluis 提醒我原子组! 为了优化起见,它非常适合这里(如果必须回溯,则该模式永远无法匹配。)
我还添加了一个额外的
[^&]+
组以避免重复负向前瞻很多次。或者,可以使用 所有格量词,它本质上做同样的事情(你的正则表达式引擎可能不支持它):
如您所见,它稍短。
I don't see how matching one is different from the other? You're just looking for exactly what you just wrote, making the portion that is
doesntmatter.com
the part you capture. I guess matching for anything until"
(not"
?) can present a problem, but you do it like this in regex:It essentially means:
"""
The complete regular expression would be:
This is more efficient than using a non-greedy expression.
Credit to Daniel Vandersluis for reminding me of the atomic group! It fits nicely here for the sake of optimization (this pattern can never match if it has to backtrack.)
I also threw in an additional
[^&]+
group to avoid repeating the negative look-ahead so many times.Alternatively, one could use a possessive quantifier, which essentially does the same thing (your regex engine might not support it):
As you can see it's slightly shorter.