Remove first p tag and last / p string tag with jQuery

3

I have a string with several HTML tags. I want to remove only the first tag <p> and the last tag </p> of this string. I've found solutions that remove all tags but these do not work.

Example:

<p> 
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  <p> Nullam feugiat, turpis at pulvinar vulputate</p> 
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</p>

Should stay:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
<p> Nullam feugiat, turpis at pulvinar vulputate</p> 
 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    
asked by anonymous 17.08.2016 / 17:01

1 answer

3

I think this will solve!

var s = "<p>uma coisa<p>outra coisa</p></p>";
s = s.substring(s.indexOf("<p>") + 3, s.lastIndexOf("</p>"));

Now if the text does not always contain the <p> tag, then you will need to do some additional checks.

    
17.08.2016 / 17:39