Themer
Themer

Reputation: 185

Remove first occurance of <p> from string

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>

Upvotes: 1

Views: 1529

Answers (3)

charlietfl
charlietfl

Reputation: 171669

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()

Upvotes: 0

Jon Glazer
Jon Glazer

Reputation: 785

str=str.replace("<p>","").replace("<\/p>","");

Hope this helps.

Upvotes: 2

Laurianti
Laurianti

Reputation: 975

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>

Upvotes: 5

Related Questions