RobLab - A script to remove all HTML tags

Published on Tuesday, July 26th, 2005

I mentioned this in a comment I wrote recently, so I thought it was time to add a script to RobLab that removes all HTML tags from an input string.

Enjoy! :-)

Posted in JavaScript | 9 comments

9 comments

  • Egor Kloos
    July 26th, 2005 at 13:41

    This always makes me wonder how I store my own content. WYSIWYG editors in CMS systems like FCKEditor asume that content is always stored with it’s layout formatting when in many cases this in not desired. Handy script, I’ll give it a look see.

  • Robert Nyman - author
    July 26th, 2005 at 13:48

    Hi Egor,

    That was pretty much the reason for me writing this script to begin with. I needed to extract some text without formatting and it was stored with HTML tags mixed in it.

    Hopefully it helps you out when needed! :-)

  • Lim Chee Aun
    July 26th, 2005 at 14:06

    If I’m not mistaken, you forgot to put more &s to the &’s, for the code example on that page:

    /*
    This line is optional, it replaces escaped brackets with real ones,
    i.e. is replaced with >
    */

  • Robert Nyman - author
    July 26th, 2005 at 14:26

    Lim,

    Thanks for your comment.

    If you mean that the source code of the example page was invalid due to unescaped ampersands, you’re completely right. I’ve fixed that now.
    Thanks!

  • Lim Chee Aun
    July 27th, 2005 at 3:08

    Uh, not I mean, there’s one line there sounds a bit weird:
    i.e. < is replaced with < and > is replaced with >

    Somehow your commenting system here seems to have problems with HTML entities.. :-) Sorry

  • Robert Nyman - author
    July 27th, 2005 at 8:53

    Lim,

    Ah, now I know what you mean. Yes, that looked a bit misleading, didn’t it? :-)

    It’s corrected now, and reads:
    &lt; is replaced with < and &gt; is replaced with >

    And yes, my commenting system can do some weird HTML escaping things sometimes, sorry. :-)

  • Hakan Bilgin
    October 30th, 2005 at 2:41

    I prefer extending the string object with a new method:

    String.prototype.stripHTML = function() {return this.replace(//g, ”);}

    Exampe:

    var foo = ‘hello robert…’;

    alert(foo.stripHTML());

    Less code, more handy…
    /hbi

  • Hakan Bilgin
    October 30th, 2005 at 2:43

    Hmm…My regular expression was deleted in the submission… a new try?

    String.prototype.stripHTML = function() {return this.replace(/<.*?>/g, ”);}

  • Robert Nyman - author
    October 30th, 2005 at 13:13

    Hakan,

    Absolutely, that’s a nice approach!

    Sorry about the code stripping, good that you made it with your second comment.
    :-)

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview

Top results