Replace html tag <span class= by <p class=

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>


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

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>


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>