-2

Below is some code I've been messing with that basically takes a string containing HTML and removes elements with class="normal". To do so, the prototype function removes all line breaks, etc before it actually removes an element and all of its inner contents. The below code works as expected:

var msg = '<p>Keeping this paragraph</p>\n<p class="normal">Removing this paragraph<br>\n</p>\n<p class="normal">Another paragraph to remove<br>\n</p>';

String.prototype.takeOut = function() {   
    return this.replace(/(\r\n|\n|\r)/gm," ")
           .replace(/<p[^>]*class="normal"[^>]*>(.*?)<\/p>/g, '');
};

alert(msg.takeOut());

My problem is that I want to pass an argument through the method like so..

msg.takeOut('normal')

and let the prototype function take that argument to remove elements with whatever class we want like so:

msg.takeOut('foo')
msg.takeOut('bar')

Here is an example with more details:

http://jsfiddle.net/sc276v98/

I know that "new RegEx()" might be the solution, but how do I incorporate that into what already works? No jQuery please! Thanks for your help!

sdawson
  • 39
  • 4
  • Don't manipulate HTML with regexp. –  Jul 03 '15 at 19:52
  • @torazaburo: What would you suggest to format a bunch of stuff? I want to build an email template where there are different paragraphs but only some of them are included depending on what type of user it is sending to..

    Admin content here

    User content here

    As the emails are being sent out in the back-end, I need something that says "if its a user, remove admin stuff.. and vice-versa". Would I be better off not using classes and instead trying something like a square bracket shortcode? Ex: [admin]admin stuff[/admin] [user]userstuff[/user].
    – sdawson Jul 03 '15 at 20:10
  • I would use a templating system. –  Jul 03 '15 at 20:12
  • Ahhh ok.. I'm going to try jade with res.render(). I've gotta do this without a DOM as well. – sdawson Jul 03 '15 at 21:06

2 Answers2

0

Based on the jsfiddle code, try:

String.prototype.takeOut2 = function(i) {
  var regExp = new RegExp('<p[^>]*class="'+i+'"[^>]*>(.*?)<\/p>', 'g');
  return this.replace(/(\r\n|\n|\r)/gm," ")
           .replace(regExp, '');
};
Mindastic
  • 4,023
  • 3
  • 19
  • 20
  • That's horrible. Who's going to maintain that? –  Jul 03 '15 at 20:18
  • Thanks for taking the time to answer. It does what I asked, but unfortunately, I didn't realize that the entire approach was bad technique! – sdawson Jul 03 '15 at 20:25
0

Since Mindastic already has a working solution using Regex, this is just an alternative, using the nodes:

var msg = '<p>Keeping this paragraph</p>\n<p class="normal">Removing this paragraph<br>\n</p>\n<p class="normal">Another paragraph to remove<br>\n</p>';

String.prototype.takeOut = function(x) {   
    var temp = document.createElement('div'), targets = temp.getElementsByClassName(x);
    temp.innerHTML = this;
    while(targets.length){ temp.removeChild(targets[0]); }
    return temp.innerHTML;
};

alert( msg.takeOut('normal') );
Community
  • 1
  • 1
blex
  • 24,941
  • 5
  • 39
  • 72
  • 1
    temp.innerHTML=this is a neat trick! This makes perfect sense. @torazburno's suggestion above is correct that I should probably use a template engine but I just need something quick and dirty that takes stuff out of existing html. – sdawson Jul 03 '15 at 20:23