use*_*510 1 html javascript regex jquery strip
我是Regex的新手,希望有人可以帮我解决以下问题:
我有一个长字符串保存为变量.该字符串包含纯文本和HTML标记,包括.p标签.
如何使用Regex从我的字符串中删除所有p标记,包括p标记上的类和ID,但不会丢失文本内部?字符串变量可以包含一个,多个或没有p标签,但始终至少包含一些文本.
示例: 现在看起来如何:
var str = Some awesome text with a <p class='myClass'>new paragraph</p> and more text.
Run Code Online (Sandbox Code Playgroud)
应该如何看待:
var str = Some awesome text with a new paragraph and more text.
Run Code Online (Sandbox Code Playgroud)
蒂姆,谢谢你的帮助.
result = str.replace(/(<p[^>]+?>|<p>|<\/p>)/img, "");
Run Code Online (Sandbox Code Playgroud)
更新的正则表达式
基于这个答案,在jQuery中很容易做到.
var str = "Some awesome text with a <p class='myClass'>new paragraph</p> and more text.";
var $temp = $("<div>").html(str);
$temp.find("p").each(function() { $(this).replaceWith(this.childNodes); });
var output = $temp.html();
Run Code Online (Sandbox Code Playgroud)