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.

Share Improve this question edited Aug 25, 2014 at 13:34 User999999 asked Aug 25, 2014 at 13:28 User999999User999999 2,5207 gold badges42 silver badges63 bronze badges 7
  • 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
 |  Show 2 more ments

1 Answer 1

Reset to default 5

Use 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