Cornwell
Cornwell

Reputation: 3410

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. 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

Thank you

Upvotes: 0

Views: 39

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You misses a ? . .* is greedy by default, you need to make it as non-greedy by adding ? next to .*.

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

Upvotes: 2

Related Questions