0

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

Cornwell
  • 3,304
  • 7
  • 51
  • 84

1 Answers1

2

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

<span class="placeholder" code="(.*?)">.*?<\/span>
                                         ^
                                         |
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    I see you're all about regular expressions, but you're helping people write bad code. I'm disappointed. – Mulan Jun 19 '15 at 17:34
  • @naomik ya, but I don't know what to do with these kind of people. I often use dupe hammer for these type of question but some says that I know the consequences of parsing html with regex. If I failed to post answer for this, someone will surely come and post an answer like this. If he knows the consequences then it will be ok.. – Avinash Raj Jun 19 '15 at 17:39
  • For me, it's easy to earn reps. But what to do, wrongly touched a easy and bad question. – Avinash Raj Jun 19 '15 at 17:41
  • 1
    Yeah, but honest reputation comes from giving good answers that contain actual good advice. You are meant to help the learner make the right choice. Instead, you provided a regexp answer and didn't help the asker learn anything. You've only helped him/her learn to do something incorrectly. – Mulan Jun 19 '15 at 17:49
  • 1
    What's the point of that 80K+ reputation if you aren't actually helping people learn the *right* way. It's disheartening for other members of the community see this kind of behavior. Sometimes it takes me a very long time to write an answer that actually serves to teach the asker. But in the end, he/she is thankful to learn right vs wrong. – Mulan Jun 19 '15 at 17:52
  • @naomik there are already many answers here about greedy and non-greedy. A smple search on that will give the thing he want. I'm ready to explain if any one of my answers won't be able to understand. – Avinash Raj Jun 19 '15 at 17:54
  • 1
    As a teacher, you have a responsibility with the knowledge you have. Someone asking a question that seems incredibly simple to you is evidence that the asker is probably very new to the languages/tools used in the question. It's very harmful for you to provide cheap answers at this stage in the learner's journey. To worsen the situation, it makes it much harder for other teachers to undo the damage done by reckless, earn-some-easy-rep teachers like yourself. – Mulan Jun 19 '15 at 17:54
  • 1
    Does it seem like the asker has any knowledge of the term/meaning of "greedy" as it relates to regexp? That's my point. You haven't done anything to teach the learner anything. Even though you *still* used regexp to parse HTML, which is an awful premise, your answer is still a "just do this and all of your problems go away" type of answer. What have you *taught* the asker? How are they better equipped to solve future problems? – Mulan Jun 19 '15 at 17:57
  • 1
    Anyway, it must be said. Yeah, it takes a long time to type all this crap up, and it takes a lot of courage to confront a user with 80K+ reputation, but here I am, taking the time to explain something to you with the hopes that you might actually *learn* something. I waited for someone to speak out, but it wasn't happening, so here I am... – Mulan Jun 19 '15 at 17:58
  • @naomik what's your problem? Is this because of answer these type of questions or answers without explanation. I saw many who posted many answers without explanation. But I add some points to my answer. If op asks for explanation then I'll surely teach him. – Avinash Raj Jun 19 '15 at 18:05