正则表达式问题

发布于 2024-09-26 18:14:54 字数 351 浏览 0 评论 0原文

演示 我正在测试一个正则表达式,我对匹配结果感到好奇,

符号*是绿色的,在我的选择中,结果应该只匹配1个结果, 如下:

<script language=javascript>
ati('#', '../../../UpLoadFile/Product/20101010162153846.jpg', '加厚青色围脖');
}
</script>
<script language=javascript>
ati

但结果不是我所期望的,任何人都可以帮我解释一下,谢谢?

demo
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-10-03 18:14:54

* 是贪婪的,但它只匹配 \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)*

自演自醉 2024-10-03 18:14:54

它无法匹配您想要的内容,因为您只匹配它的开头。

当您在 2 个标记之间查找任意内容时,贪婪就会得到您,并且您会获得第一个和最后一个之间的所有内容,即

/<p>.*<\/p>/

通常,当您这样做时,您需要所有 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.

/<p>.*<\/p>/

Usually, when you do that, you want all p elements. But without the ungreedy .*? or the U flag, you will get everything between the first and last instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文