正则表达式问题
我正在测试一个正则表达式,我对匹配结果感到好奇,
符号*是绿色的,在我的选择中,结果应该只匹配1个结果, 如下:
<script language=javascript>
ati('#', '../../../UpLoadFile/Product/20101010162153846.jpg', '加厚青色围脖');
}
</script>
<script language=javascript>
ati
但结果不是我所期望的,任何人都可以帮我解释一下,谢谢?
i'm test a regular expression,i curious about the match result,
the symbol * is greendy,in my option,the result shuld only match 1 result,
like belows:
<script language=javascript>
ati('#', '../../../UpLoadFile/Product/20101010162153846.jpg', '加厚青色围脖');
}
</script>
<script language=javascript>
ati
but the result is not what i expect,any one could help me explain it,thank you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
*
是贪婪的,但它只匹配\s
,即空白字符。如果您想要匹配直到 ati 最后一次出现为止的所有内容,请改用.*
。如果此环境与 . 不匹配换行符,也许您可以通过匹配
(.|\n)*
来包含它们*
is greedy, but it's only matching\s
, which means whitespace characters. If you want to match everything up to the last appearance of ati, use.*
instead.If this environment doesn't match newlines with ., maybe you can include them by matching on
(.|\n)*
它无法匹配您想要的内容,因为您只匹配它的开头。
当您在 2 个标记之间查找任意内容时,贪婪就会得到您,并且您会获得第一个和最后一个之间的所有内容,即
通常,当您这样做时,您需要所有
p
元素。但是如果没有不贪婪的.*?
或U
标志,您将获得第一个实例和最后一个实例之间的所有内容。It can not match what you want because you are only matching the start of it.
Greedy gets you when you look for something arbitrary between 2 markers, and you get everything between the first and last, i.e.
Usually, when you do that, you want all
p
elements. But without the ungreedy.*?
or theU
flag, you will get everything between the first and last instance.