Strip HTML Tags in JavaScript

var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");

Comments

  1. User Avatar
    Dango
    Permalink to comment#

    Your script works great! Cheers!

  2. User Avatar
    admire
    Permalink to comment#

    this is so cool , i like it

  3. User Avatar
    John Doe
    Permalink to comment#
    /**
    * isArray
    *
    * @param mixed input
    * @return bol
    */
    
    function is_array(obj) {
    if (obj.constructor.toString().indexOf(‘Array’) == -1) {
    return false;
    }
    return true;
    }
    
    /**
    * stripTags
    *
    * @param mixed input
    * @parm mixed output
    */
    
    function strip_tags(input) {
    if (input) {
    var tags = /(]+)>)/ig;
    if (!is_array(input)) {
    input = input.replace(tags,”);
    }
    else {
    var i = input.length;
    var newInput = new Array();
    while(i–) {
    input[i] = input[i].replace(tags,”);
    }
    }
    return input;
    }
    return false;
    }
    
  4. User Avatar
    Pushpinder Bagga
    Permalink to comment#

    function strip(html)
    {
    var tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    return tmp.textContent || tmp.innerText;
    }

    • User Avatar
      JC
      Permalink to comment#

      This was even better for my needs. No issues with special characters etc…

    • User Avatar
      Morgan Roderick
      Permalink to comment#

      That is awful advice!

      If for some reason (like malicious intent of users) the html argument contains a script tag, you’ve now opened up for XSS attacks!!!

      Don’t use the DOM for something that doesn’t require it.

      Also, the DOM is really slow.

    • User Avatar
      Martin Adámek

      This solution is great for using of inner content from paragraph in JS Alert window – it strips nbsp and em efectivelly,
      thanks

    • User Avatar
      Venkat
      Permalink to comment#

      Pushpinder,
      Lovely. Worked great

  5. User Avatar
    derek
    Permalink to comment#

    hey!!!..this is so ridiculous..

  6. User Avatar
    Eugene
    Permalink to comment#

    Thank you for great example

  7. User Avatar
    Sebastian
    Permalink to comment#

    Thanks, this does exactly what I need (and so concisely, too!)

  8. User Avatar
    Ian
    Permalink to comment#

    Thanks! A quick note about the regexp: the “i” isn’t needed here because there are no characters to be case-insensitive about. However, it does exactly what you want either way.

  9. User Avatar
    Porter
    Permalink to comment#

    Nice, but the parentheses are unnecessary.

    .replace(/<[^>]+>/ig,””);

  10. User Avatar
    Florian Ricard
    Permalink to comment#

    Hi :)

    I saw your contact form and i must say i love it!
    Do you have a tutorial or something like that? It’s a wonderful one :)^
    Hope to hear some news of you,

    A french reader,

    Florian

  11. User Avatar
    Truong Duong
    Permalink to comment#

    Thank for script :)

    @Ricard: If you want to make a copy of the contact form, just view source or save this page to you local ;)

  12. User Avatar
    jeu-jeu-jeu.net
    Permalink to comment#

    beautul site thank you for great example

  13. User Avatar
    DScout
    Permalink to comment#

    the /i for case insensitivity is definitely recommended.
    When using contenteditable, IE produces upper case tags, mozilla would only create lower case… To strip those you need it case insensitive.

    • User Avatar
      do0g

      DScout, this is incorrect. There are no specified alphabetical characters in the regular expression – the case insensitivity modifier therefore affects nothing.

  14. User Avatar
    Sadia
    Permalink to comment#

    Hi

    I have following code:

    var text = ‘[$ ssIncludeXml(docName,”wcm:root/wcm:element[@name=’innerpage_content’]/text()”) $]’;
    var StrippedString = text.replace(/(]+)>)/ig,””);

    where ‘[$ ssIncludeXml(docName,”wcm:root/wcm:element[@name=’innerpage_content’]/text()”) $]’
    is Idoc script that brings a block of HTML from a placeholder. But i am getting “unterminated string literal” Error at first line.

    What i want to do is to remove or strip all HTML tags and to get plain text out of that markup.

    Kindly let me know if there is any solution.

    Thanks

  15. User Avatar
    JOhn
    Permalink to comment#

    works great but doest strip whitespaces….  

  16. User Avatar
    Valutar BNR
    Permalink to comment#

    Thank you! It was very useful for me and I think that is useful for everyone.
    Thank you again!

  17. User Avatar
    Elliott
    Permalink to comment#

    Yeah, this solution removed all sorts of HTML, paragraph, line breaks, in-line styles etc etc

  18. User Avatar
    reena upadhyay
    Permalink to comment#

    This does not works for IE. Please provide solution to strip tag in javascript that works for all browsers

  19. User Avatar
    Shilpa Agrawal
    Permalink to comment#

    Thanks for this script
    It work greate

  20. User Avatar
    Ammar
    Permalink to comment#

    i am trying it on

    var message;

        firstName = document.getElementById("username").value;
    
        if (firstName == null || firstName == "" || firstName == NaN || firstName == "First Name") {
            message = "Please Add some name.";
            document.body.insertAdjacentHTML("BeforeEnd", "" + message + "");
        }
        else {
            if (document.getElementById("myMessage")) {
                debugger;
                arguments = document.getElementById("myMessage").value.replace(/(]+)>)/ig, "");
            }
        }
    

    but it is not working and saying

    cannot call method ‘replace’ of undefined

  21. User Avatar
    Ryan Mc Closkey

    Was wondering how this would be implemented if I only wanted to remove the href tags from a string of text, instead of removing all the tags? I’m trying to retrieve a page of text from a website but I only want the plain text with the formatting tags (p, ul, li).
    Hope this makes sense, thanks in advance.

  22. User Avatar
    DropTheNerd

    This was excellent! Thanks!

  23. User Avatar
    javier
    Permalink to comment#

    Great! Thanks!

  24. User Avatar
    Hemant Vaniya
    Permalink to comment#

    Thanks,
    Its working fine.

  25. User Avatar
    Emmanuel Sayson
    Permalink to comment#

    Cool! This is perfectly working…

  26. User Avatar
    anonymous
    Permalink to comment#

    What about < b r / > or < h r / > (the self closing tags) ?

  27. User Avatar
    Nagarjuna Gottimukkala
    Permalink to comment#

    Cool……Nice Example.

  28. User Avatar
    Jeremy
    Permalink to comment#

    Looks like “newInput” doesn’t do anything at all? So it’s either extraneous or there’s a problem with the code.

  29. User Avatar
    Hardik Sondagar

    I have developed same thing using javascript Regular Expression.
    It’ll strip all the html tags excluding tag provided in exclude list by user.
    source code is also available on github
    check here. HTML Tag Stripper

  30. User Avatar
    Germano

    Nice, but it’s not that safe… I’d rather use jQuery:

    $("<div/>").text('<img alt="a>b" src="a_b.gif" />').text();

  31. User Avatar
    Ahahahaha

    document.body.innerText

    <a onclick=”return a > b”> ~ fail

  32. User Avatar
    SKV
    Permalink to comment#

    But this code is not working well with HTML table content.

  33. User Avatar
    Al
    Permalink to comment#

    How can strip all tags except anchor and img tags?

  34. User Avatar
    Jonas
    Permalink to comment#

    You can easily leave out the case sensitivity /i and the grouping ():

    var noHtml = hasHtml.replace(/<[^>]+>/ig, '')
    
  35. User Avatar
    duromir
    Permalink to comment#

    using jQuery
    jQuery(stringWithTags).text()

  36. User Avatar
    farzad
    Permalink to comment#

    jQuery(stringWithTags).text();
    it is what i want. tanx…

  37. User Avatar
    Muhammad Navaid
    Permalink to comment#

    not working with AngularJS.

  38. User Avatar
    Mohammad Mustafa Ahmedzai
    Permalink to comment#

    Probably the simplest probably I found online. Thanks a bunch for it. Worked just fine!

  39. User Avatar
    Hamada Abdelaziz
    Permalink to comment#

    this is the best solution i have find
    http://phpjs.org/functions/strip_tags/
    this is equivalent to PHP strip_tags function

  40. User Avatar
    cccccccccc
    Permalink to comment#
    string.replace(/\n/g, "");
    string.replace(/[\t ]+\</g, "<");
    string.replace(/\>[\t ]+\</g, "><");
    string.replace(/\>[\t ]+$/g, ">");
    
  41. User Avatar
    Samantha
    Permalink to comment#

    Doesn’t anyone see how this solution greatly affects this text:

    Rounded amounts < 3 are way easier for people to use in calculations, since they are so tiny than numbers that are >=3

    Becomes: Rounded amounts =3

  42. User Avatar
    mindfullsilence
    Permalink to comment#

    Safe way to use the DOM to strip html.

    function striptags(content) {
      var frag = document.createDocumentFragment();
      var innerEl = document.createElement('div');
      frag.appendChild(innerEl);
      innerEl.innerHTML = content;
      return frag.firstChild.innerText;
    }
    striptags('<script>alert("xss attack!")</script>');
    

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag