admin管理员组文章数量:1410689
My apologies if the question has been asked before, but so far my stackoverflow search didn't bring me quite the answer i needed. At undetermined intervals my JavaScript is receiving a string containing HTML.
An simplistic example:
<p class='mentator'>Person A</p> Comment of Person A <br/> <p class='mentator'> Person B </p> Comment of person B
When some conditions are met then all the <p>
tags and their content should be removed from the string. I know how to remove the <p>
tags using the following code:
stringComments= stringComments.replace(/<\/?p[^>]*>/g, "");
How can i modify that regex to include the content of the <p>
tags? (regex= .*?)
My expected output should be as follows:
Comment of Person A <br/> Comment of person B
Note: those referring to jQuery's remove()
. That won't work, first of because its not part of DOM yet & secondly the changes must me limited to this string.
My apologies if the question has been asked before, but so far my stackoverflow search didn't bring me quite the answer i needed. At undetermined intervals my JavaScript is receiving a string containing HTML.
An simplistic example:
<p class='mentator'>Person A</p> Comment of Person A <br/> <p class='mentator'> Person B </p> Comment of person B
When some conditions are met then all the <p>
tags and their content should be removed from the string. I know how to remove the <p>
tags using the following code:
stringComments= stringComments.replace(/<\/?p[^>]*>/g, "");
How can i modify that regex to include the content of the <p>
tags? (regex= .*?)
My expected output should be as follows:
Comment of Person A <br/> Comment of person B
Note: those referring to jQuery's remove()
. That won't work, first of because its not part of DOM yet & secondly the changes must me limited to this string.
- 4 Nice, parsing HTML with regexes :D – Cerbrus Commented Aug 25, 2014 at 13:30
- Are you happy to use jQuery? – Tom Fenech Commented Aug 25, 2014 at 13:31
-
You can use jQuery. It doesn't have to be part of the dom. Just do something like
var e = $(mystringofhtml)
. – Matt Burland Commented Aug 25, 2014 at 13:32 - Given your "simplistic example", what exactly do you expect the output to be? – Matt Burland Commented Aug 25, 2014 at 13:33
-
@MattBurland Added the needed output based on the example. Using
$(mystringofhtml)
can i target classes or is the string interpreted as a string (non html) – User999999 Commented Aug 25, 2014 at 13:35
1 Answer
Reset to default 5Use jQuery - don't try to parse HTML with regex, it'll give you no end of trouble. There are lots of ways to do it, but here's one way:
var s = "<p class='mentator'>Person A</p> Comment of Person A <br/> <p class='mentator'> Person B </p> Comment of person B";
var elem = $("<div>" + s + "</div>");
var p = elem.find("p");
p.remove();
console.log(elem[0].innerHTML);
Logs:
Comment of Person A <br> Comment of person B
http://jsfiddle/a565cowm/
You don't need to add something to the DOM for jQuery to work on it. jQuery can work with disconnected fragments of HTML.
To be safer, you might even want to use a selector to target the class rather than the <p>
tag. That way, if it changes to some other tag, or <p>
tags get added to the content you do want, you won't accidentally end up removing the wrong part.
var p = elem.find(".mentator");
Edit: For pleteness, it should be noted that this isn't a trick limited to jQuery. You can do the same thing in vanilla JS (browser inpatibilities not withstanding):
var div = document.createElement("div");
div.innerHTML = s;
var p = div.getElementsByTagName("p"); // or getElementsByClassName if you prefer
while(p.length > 0) {
div.removeChild(p[0]);
}
console.log(div.innerHTML);
http://jsfiddle/a565cowm/1/
本文标签: javascriptRemove content of ltpgttags from stringStack Overflow
版权声明:本文标题:javascript - Remove content of <p>-tags from string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744801553a2625891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论