Take the 2-minute tour ×

Possible Duplicate:
Strip HTML from Text JavaScript

How can I strip the HTML from a string in JavaScript?

share|improve this question
add comment

marked as duplicate by James, Mihai Iorga, Baz, Dominic Kexel, Sergey K. Sep 28 '12 at 9:30


4 Answers

up vote 36 down vote accepted

Using the browser's parser is the probably the best bet in current browsers. The following will work, with the following caveats:

  • Your HTML is valid within a <div> element. HTML contained within <body> or <html> or <head> tags is not valid within a <div> and may therefore not be parsed correctly.
  • textContent (the DOM standard property) and innerText (non-standard) properties are not identical. For example, textContent will include text within a <script> element while innerText will not (in most browsers).
  • The HTML does not contain <script> elements.
  • The HTML is not null
  • The HTML comes from a trusted source. Using this with arbitrary HTML allows arbitrary untrusted JavaScript to be executed. This example is from a comment by Mike Samuel on the duplicate question: <img onerror='alert(\"could run arbitrary JS here\")' src=bogus>


var html = "<p>Some HTML</p>";
var div = document.createElement("div");
div.innerHTML = html;
var text = div.textContent || div.innerText || "";
share|improve this answer
Nice answer, I didn't know about textContent. How many browsers do textContent + innerText cover? BTW, I've edited my answer to include the jQuery way. –  Felix Feb 15 '11 at 11:04
@Felix: All major browsers have at least one of textContent and innerText. –  Tim Down Feb 15 '11 at 11:05
Doesn't work when the string contains something like <script>alert('hi');</script>. Then it crashes with "illegal token at" etc.. –  Till Aug 19 '12 at 1:04
@Till: Good point. I'll amend my answer. –  Tim Down Sep 7 '12 at 14:52
Good caveats. In case it is not already clear I wanted to add that Firefox will crash on div.innerHTML = html if the value of html is NULL. Worse, it won't properly report the error (instead says parent function has TypeError). Chrome/IE do not crash. –  Ryan Rapp Jan 24 '13 at 22:20
add comment
cleanText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");

Distilled from this website

share|improve this answer
Sorry, but that would break <img alt="a>b" src="a_b.gif" /> –  f.ardelian Feb 15 '11 at 10:56
Ah, well spotted. Apologies. –  ReactiveRaven Feb 15 '11 at 11:41
@f.ardelian people who make a hobby out of breaking the ill-use of regular expressions for parsing general HTML are great. It is a great hobby. –  Ziggy May 7 '13 at 18:39
@Ziggy: That sounds an awful lot like sarcasm... –  f.ardelian May 7 '13 at 22:31
@f.ardelian no! Really! Every time I read one of these comment threads I get a little thrill. "Ho ho ho," I think "<img alt=\"a>b\" src=\"a_b.gif\" />, so clever!" –  Ziggy May 8 '13 at 5:28
show 1 more comment
var html = "<p>Hello, <b>World</b>";
var div = document.createElement("div");
div.innerHTML = html;
alert(div.innerText); // Hello, World

That pretty much the best way of doing it, you're letting the browser do what it does best -- parse HTML.

Edit: As noted in the comments below, this is not the most cross-browser solution. The most cross-browser solution would be to recursively go through all the children of the element and concatenate all text nodes that you find. However, if you're using jQuery, it already does it for you:

alert($("<p>Hello, <b>World</b></p>").text());

Check out the text method.

share|improve this answer
Not every browser supports innerText. –  Tim Down Feb 15 '11 at 10:45
A concise jQuery could look like: var html = "<b>test</b>"; var text = $("<div/>").html(html).text(); Using $("<div/>") lets you reuse the same element and less memory for consecutive calls or for loops. –  Sukima Jan 4 '12 at 21:14
Same problem, crash it with: $(...).html('<script>alert("hi");</script>').text(); –  Till Aug 19 '12 at 1:05
and check out the text method for var txt = "<p>my line</p><p>my other line</p>some other text"; alert($(txt).text(); where you don't proxy the string within a dom node. 3 lines in, 2 lines out. –  frumbert Oct 17 '12 at 2:49
add comment

I know this question has an accepted answer, but I feel that it doesn't work in all cases.

For completeness and since I spent too much time on this, here is what we did: we ended up using a function from php.js (which is a pretty nice library for those more familiar with PHP but also doing a little JavaScript every now and then):


It seemed to be the only piece of JavaScript code which successfully dealt with all the different kinds of input I stuffed into my application. That is, without breaking it – see my comments about the <script /> tag above.

share|improve this answer
^ this, definitely better than the accepted answer for Chrome 30.0 and above –  ebt Oct 4 '13 at 20:03
add comment
