1

Below is the code

 var str = '<p>first occurrence</p> can have multiple lines of ln or new line code etc <p>another p</p> and then again <p>another code in p </p>';

expected result with regex or simple jquery:

 first occurrence can have multiple lines of ln or new line code etc <p>another p</p> and then again <p>another code in p </p>
Themer
  • 185
  • 1
  • 12

3 Answers3

5

var str = document.getElementById('test').innerHTML.trim();
// var str = '<p>first occurrence</p> can have multiple lines of ln or new line code etc <p>another p</p> and then again <p>another code in p </p>';

str = str.replace(/<p>(.*?)<\/p>/, '$1');
 
console.log(str);
<div id="test">
 <p>first occurrence</p> can have multiple lines of ln or new line code etc <p>another p</p> and then again <p>another code in p </p>
</div>
Laurianti
  • 903
  • 1
  • 5
  • 19
2
str=str.replace("<p>","").replace("<\/p>","");

Hope this helps.

Jon Glazer
  • 783
  • 1
  • 10
  • 25
0

Don't use regex on html ... put the string into a newly created element and manipulate that element

var $div = $('<div>').html(str);
$div.find('p:first').unwrap()
str = $div.html()
Community
  • 1
  • 1
charlietfl
  • 170,828
  • 13
  • 121
  • 150