从字符串中删除<p>的第一次出现

时间:2017-02-03 18:00:02

标签: jquery regex

以下是代码

 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>';

使用正则表达式或简单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>

3 个答案:

答案 0 :(得分:4)

&#13;
&#13;
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);
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

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

希望这有帮助。

答案 2 :(得分:0)

Don't use regex on html ...将字符串放入新创建的元素中并操纵该元素

var $div = $('<div>').html(str);
$div.find('p:first').unwrap()
str = $div.html()