3

I have this html tags: <span class="text_obisnuit"></span> and I must change it with <p class="text_obisnuit"></p>

</p>
        <span class="text_obisnuit"> I must choose</em>"  between normal and pathological,  which most of the times is equivalent with  the collapse.</span>

Must Become:

</p>
        <p class="text_obisnuit"> I must choose</em>"  between normal and pathological,  which most of the times is equivalent with  the collapse.</p>

I made a regex, but I don't know why is not working:

SEARCH: </p>\s+.*\K<span class="text_obisnuit">.*(</span>)

REPLACE BY: <p class="text_obisnuit">$1</p>

Also, the regex must NOT find and replace this kind of lines, only the first case.

<p><span class="text_obisnuit2">* Goe:</span><span class="text_obisnuit"> Sansy - <em>Must Heva/em>, Publishing House, 1986.</span><br/></p>

3
  • 2
    Is this what you want? Find what: </p>\s+.*\K<span class=("text_obisnuit">.*)</span>, Replace with: <p class=$1</p>, check Wrap around Jul 19, 2021 at 7:20
  • yes, thanks @Reddy Lutonadio . Please put your code as an answer. So I can vote it.
    – Just Me
    Jul 19, 2021 at 7:25
  • 5

3 Answers 3

4

Your pattern is only capturing this group (</span>), not the content. That is why it is not working as intended when you replace. Try the following:

  • Find what: </p>\s+.*\K<span class=("text_obisnuit">.*)</span>
  • Replace with: <p class=$1</p>
  • Check Wrap around
1

yes, I know what was my mistake, so this will help.

SEARCH: (?-s)</p>\s+.\K(<span class="text_obisnuit">)(.*)(</span>)

REPLACE BY: <p class="text_obisnuit">\2</p>

1

This is horrific bad use of RegEx.

See this famous answer why

Why is this bad? Because HTML tags can be nested. There's no way RegEx can ever target the right group of tags in either greedy or non-greedy mode.

Alternatively, using a CSS solution, if your goal is an issue of the display layout of the <span> tag, use this CSS to make your <span> display as block elements:

span.text_obisnuit { display: block; }

Here is my rationale why RegEx is a bad idea.

If the HTML is simple enough to verify by hand, you can easily just click the two spans and change it manually.

If it is complex enough to require RegEx, you will be unable to verify it due to heavy nesting, and that is exactly the use case where RegEx is inappropriate. Using RegEx to parse HTML is automatically a wrong answer.

Right off the bat, there are already two failure scenarios I can think of:

  • Nested <span> tags
  • <span> tags that do not have a closing tag on the same line (HTML do not render carriage returns)

Actual solution JSFiddle Link

$("span.text_obisnuit").each(function(){$(this).replaceWith($('<p class="text_obisnuit">' + this.innerHTML + '</p>'));});

enter image description here

Assuming that your <span> tags are consistently named, this is the 100% fool-proof method of changing your tags, because it is using an actual HTML parser.

If you need to copy the HTML, open the JSFiddle page in Chrome, inspect JSFiddle's output area (white background), and look for the HTML code. Use Copy Element on the outer div to get everything, and paste into an editor. The output text would look like this:

<div>
<p class="text_obisnuit">Some Text</p>

<p class="text_obisnuit">Some <span>Nested</span> Text</p>

<p class="text_obisnuit">Mutliline
Text</p>
</div>
4
  • It looks like the OP just wants to replace the text in Notepad++, not replace it in real-time with code (which of course is bad practice) Jul 20, 2021 at 2:33
  • Indeed. I post this topic because I write articles in Ms Word, then copy them directly into Dreamweaver, and the transition change some of my tags. So time to time, I must verify all my html pages, so as to make sure the tags are the one I want. It is not about css. :)
    – Just Me
    Jul 20, 2021 at 7:22
  • You say that using regex in Notepad++ is a bad thing to do. That's a valid XY problem or frame challenge. However, you do not give instructions on what they should do instead, other than just mentioning using JavaScript, nor do you link to another question that would give the answer.
    – trlkly
    Jul 20, 2021 at 7:40
  • @trlkly updated with an actual working solution on JSFiddle
    – Nelson
    Jul 20, 2021 at 8:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .