code_junkie

How can I remove an entire HTML tag (and its contents) by its class using a regex?

Nov 11th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. <div class="footer".*?>(.*?)</div>
  2.  
  3. <div[^>]*class="footer"[^>]*>(.*?)</div>
  4.  
  5. <div>
  6. <div class="footer">
  7. <div>Hi!</div>
  8. </div>
  9. </div>
  10.  
  11. document = //load document
  12. divs = document.getElementsByTagName("div");
  13. for(div in divs) {
  14. if(div.getAttributes["class"] == "footer") {
  15. parent = div.getParent();
  16. for(child in div.getChildren()) {
  17. // filter attribute types?
  18. parent.insertBefore(div, child);
  19. }
  20. parent.removeChild(div);
  21. }
  22. }
  23.  
  24. use HTML::TreeBuilder::XPath;
  25.  
  26. my $tree = HTML::TreeBuilder::XPath->new;
  27. $tree->parse_file( 'yourdocument.html' );
  28.  
  29. for my $node ( $tree->findnodes( '//*[@class="footer"]' ) ) {
  30. $node->replace_with_content; # delete element, but not the children
  31. }
  32.  
  33. print $tree->as_HTML;
  34.  
  35. <div class="footer".*?>(.*?)</div>
  36.  
  37. <([^s]+).*?class="footer".*?>([.n]*?)</([^s]+)>
  38.  
  39. <div class="footer"><b></b></div>
  40.  
  41. <div[^>]*class="footer"[^>]*>(.*?)</div>
  42.  
  43. <div[^>]*class="footer"[^>]*>(.*?)</div>
Add Comment
Please, Sign In to add comment