简体   繁体   English

只配对一组

[英]Matching only one group

I have the following text: 我有以下文字:

<p>k</p><p><span class="placeholder" code="{YSZZ}">Samsung xyz</span>&nbsp;</p>
<p>khgj&nbsp;<span class="placeholder" code="{UIDJU}">iPhone 9k</span>&nbsp;</p></div>

I want to replace the span tags with their respective code attribute. 我想将span标签替换为其各自的code属性。 For that I'm using this pattern: 为此,我使用这种模式:

/<span class="placeholder" code="(.*?)">(?:.*)<\/span>/gi

But it's matching from the first span to the last, instead of each span individually. 但这是从第一个跨度到最后一个跨度的匹配,而不是每个跨度都单独匹配。 What am I missing? 我想念什么?

https://regex101.com/r/fP4aD7/1 https://regex101.com/r/fP4aD7/1

Thank you 谢谢

You misses a ? 你错过了? . .* is greedy by default, you need to make it as non-greedy by adding ? .*默认为贪婪,您需要通过添加?使其成为非贪婪? next to .* . .*旁边。

<span class="placeholder" code="(.*?)">.*?<\/span>
                                         ^
                                         |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM