841

How to strip off HTML tags from a string using plain JavaScript only, not using a library?

JGallardo
  • 11,074
  • 10
  • 82
  • 96
Bryan
  • 17,201
  • 24
  • 97
  • 123

46 Answers46

905

If you're running in a browser, then the easiest way is just to let the browser do it for you...

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

Note: as folks have noted in the comments, this is best avoided if you don't control the source of the HTML (for example, don't run this on anything that could've come from user input). For those scenarios, you can still let the browser do the work for you - see Saba's answer on using the now widely-available DOMParser.

Black
  • 18,150
  • 39
  • 158
  • 271
Shog9
  • 156,901
  • 35
  • 231
  • 235
  • 44
    Just remember that this approach is rather inconsistent and will fail to strip certain characters in certain browsers. For example, in Prototype.js, we use this approach for performance, but work around some of the deficiencies - http://github.com/kangax/prototype/blob/a223833c8b49ae55f03b1e1a3a5b7e9fb647c139/src/lang/string.js#L476 – kangax Sep 14 '09 at 16:08
  • 12
    Remember your whitespace will be messed about. I used to use this method, and then had problems as certain product codes contained double spaces, which ended up as single spaces after I got the innerText back from the DIV. Then the product codes did not match up later in the application. – Magnus Smith Sep 17 '09 at 15:03
  • 12
    @Magnus Smith: Yes, if whitespace is a concern - or really, if you have any need for this text that doesn't directly involve the specific HTML DOM you're working with - then you're better off using one of the other solutions given here. The primary advantages of this method are that it is 1) trivial, and 2) will reliably process tags, whitespace, entities, comments, etc. in *the same way as the browser you're running in*. That's frequently useful for web client code, but not necessarily appropriate for interacting with other systems where the rules are different. – Shog9 Sep 17 '09 at 21:05
  • Requisite reference on this topic: http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox/1359822#1359822 – Crescent Fresh Dec 22 '09 at 13:56
  • @Tom: [seems to work just fine](http://jsbin.com/udebu3/) in the IE6 I test with. – Shog9 Jan 14 '11 at 17:55
  • 244
    Don't use this with HTML from an untrusted source. To see why, try running `strip("")` – Mike Samuel Sep 22 '11 at 18:06
  • 3
    This will also strip any
    's between text. And that could concatenate the text together, if there should not be any blank spaces between the
    's the preceding text and/or the following text
    –  Jan 30 '13 at 06:41
  • 28
    If html contains images(img tags), the images will be requested by the browser. That's not good. – douyw Feb 13 '13 at 06:47
  • This will also strip string using `<`: `a – Andreas Louv Apr 03 '13 at 08:50
  • @MikeSamuel maaaaaan Mike Samuel, is there a safe way to strip html from something? – Ziggy May 07 '13 at 18:33
  • 2
    @Ziggy: if you need to sanitize *untrusted* HTML using JavaScript, you might want to start here: http://stackoverflow.com/questions/295566/sanitize-rewrite-html-on-the-client-side – Shog9 May 07 '13 at 18:36
  • What about using the answer from this: http://stackoverflow.com/questions/822452/strip-html-from-text-javascript – Ziggy May 07 '13 at 18:37
  • @Ziggy, Doesn't that link point to this thread? Shog9's link to the caja sanitizer is the best approach IMHO. – Mike Samuel May 08 '13 at 00:23
  • appreciate that you did not use jquery – user151496 Jul 23 '15 at 09:31
  • Don't forget to use trim() – Jaskaran Singh Jan 26 '18 at 11:45
  • 1
    @MikeSamuel The problem is the first two lines, though, not the last line: What is unsafe is calling `Document​.create​Element` and assigning to `Element​.inner​HTML` with untrusted (HTML) input. Reading from `Node.textContent` or `Node.innerText` is safe, and you don’t even need the latter. – caw May 29 '19 at 21:05
  • @caw, I'm afraid I don't understand what you're getting at. I agree that the third line isn't problematic in isolation, but I never suggested it was, and if you want to classify individual lines as safe/unsafe, why worry about the first line? Creating a div is not unsafe. – Mike Samuel May 30 '19 at 09:22
  • You made the general statement “Don't use this with HTML from an untrusted source”, which is true, but can be made more specific, i.e. “Don't assign to `Element​.inner​HTML` from an untrusted source”. Reading `Node.textContent`, which is much more important and relevant for this question, is totally fine. – caw May 30 '19 at 15:05
  • If you need to strip only the first and last HTML tag, see: https://stackoverflow.com/a/2095792/8839237 – Peter Mar 12 '21 at 17:01
782
myString.replace(/<[^>]*>?/gm, '');
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 9
    Doesn't work for `` before injecting via `innerHTML`. – Mike Samuel Dec 24 '10 at 15:07
  • @Mike, you should do the replacement after the string has actually been finished – nickf Dec 26 '10 at 15:48
  • that is not sufficient. If two different scripts do two different writes : `document.write('/g, '');` and `document.write('')` then the second write closes the incomplete tag from the first. Also, `.` does not match `\n`, so your regex does not work on `''` which is a valid complete tag. – Mike Samuel Dec 27 '10 at 03:09
  • @MikeSamuel not quite sure your fix works correctly on the problem you suggested. If the problem you proposed is that two different writes hold parts of one tag, e.g. the first "blablabla
    ". Then while your regex will erase the first string completely, it will ignore the initial > bracket in the second string. Did I understand your test case correctly?
    – Perishable Dave Sep 22 '11 at 17:46
  • 2
    @PerishableDave, I agree that the `>` will be left in the second. That's not an injection hazard though. The hazard occurs due to `<` left in the first, which causes the HTML parser to be in a context other than [data state](http://www.w3.org/TR/html5/tokenization.html#data-state) when the second starts. Note there is no transition from data state on `>`. – Mike Samuel Sep 22 '11 at 18:04
  • 123
    @MikeSamuel Did we decide on this answer yet? Naive user here ready to copy-paste. – Ziggy May 07 '13 at 18:32
  • 1
    @Ziggy, no. This answer is still not safe even if you make the close `>` optional. Consider `""`. – Mike Samuel May 08 '13 at 00:24
  • 3
    This also, I believe, gets completely confused if given something like `` Assuming correctly written HTML, you still need to take into account that a greater than sign might be somewhere in the quoted text in an attribute. Also you would want to remove all the text inside of ` – Jonathon Aug 18 '13 at 02:37
  • 19
    @AntonioMax, I've answered this question [ad nauseam](http://stackoverflow.com/a/430240/20394), but to the substance of your question, because **security critical code shouldn't be copied & pasted.** You should download a library, and keep it up-to-date and patched so that you're secure against recently discovered vulnerabilities and to changes in browsers. – Mike Samuel Nov 27 '13 at 16:04
  • @MikeSamuel That was awesome. We're on the same page and your comment on that link is just right, thank you. – Antonio Max Nov 27 '13 at 21:59
  • Could you continue to loop through this expression until the text no longer contains any `<>`, assuming that the text you're trying to find doesn't have any either? – Vasu Jan 28 '14 at 23:39
  • 1
    The solution shown above does not replace " ". The "jQuery(html).text();" does that job. – Benny Code Feb 24 '14 at 17:38
  • This answer is far better than others as I think anyone shall be able use and add other characters that they want or might want to remove. If any data is coming from database with escape characters like &lt, &gt, etc. The regular expression is the only way that is properly applicable in all the cases. – Abhishek Dhanraj Shahdeo Nov 08 '16 at 10:47
  • 1
    suppose there are html comment in tag then how this will work? – Raghvendra Feb 16 '17 at 13:09
  • `m` in `/<(?:.|\n)*?>/gm` is not needed because you don't use `^` and `$` inside the regex. See [*"The multiline mode is enabled by the flag /.../m. It only affects the behavior of ^ and $."*](https://javascript.info/regexp-multiline-mode). I've tested and it works without `m`. – tanguy_k May 16 '19 at 12:40
  • 2
    Would be nice to have some explanations (what cases it handles, limitations, explanations about the regex itself...) and unit tests – tanguy_k May 16 '19 at 13:02
  • a simple character replace: `myString.replace(/[<]/g, '<').replace(/[>]/g, '>');` – SwiftNinjaPro Dec 15 '19 at 01:46
  • nice, short and clean – Bishoy Hanna May 11 '20 at 12:49
  • 1
    how come no one pointed to https://stackoverflow.com/a/1732454/501765 yet? – törzsmókus Jan 22 '21 at 10:52
  • @MikeSamuel I do not think the `?` you added in `/<[^>]*>?/gm` is needed or even correct. Wouldn't it replace any `<` in the text? – Agostino Feb 18 '21 at 11:27
  • @Agostino. It's necessary if you want to remove the tag from `Foo' in text that follows.</plaintext></span> –&nbsp;<a href="../../users/20394/mike-samuel" title="118,113 reputation" class="comment-user ">Mike Samuel</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment117166194_822464"><span title="2021-02-19T05:56:07.180 License: CC BY-SA 4.0" class="relativetime-clean">Feb 19 '21 at 05:56</span></a></span> </div> </div> </li> <li id="comment-117237175" class="comment js-comment " data-comment-id="117237175" data-comment-owner-id="1600770" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment117237175_822464"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@MikeSamuel Will it also strip any literal `&lt;` in the text, e.g. `3&lt;4`? If so, I would at least add a note about this in the answer.</span> –&nbsp;<a href="../../users/1600770/agostino" title="2,723 reputation" class="comment-user ">Agostino</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment117237175_822464"><span title="2021-02-22T10:25:10.347 License: CC BY-SA 4.0" class="relativetime-clean">Feb 22 '21 at 10:25</span></a></span> </div> </div> </li> <li id="comment-124388694" class="comment js-comment " data-comment-id="124388694" data-comment-owner-id="1142571" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment124388694_822464"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You might want to decode html-entities in text after removing tags (e.g. &amp;,   ). This library can help you https://www.npmjs.com/package/html-entities</span> –&nbsp;<a href="../../users/1142571/vincente" title="341 reputation" class="comment-user ">Vincente</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment124388694_822464"><span title="2021-12-15T17:02:59.867 License: CC BY-SA 4.0" class="relativetime-clean">Dec 15 '21 at 17:02</span></a></span> </div> </div> </li> <li id="comment-125817126" class="comment js-comment " data-comment-id="125817126" data-comment-owner-id="9854149" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment125817126_822464"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`myString.replace(/&lt;.*?&gt;/gm, '')`</span> –&nbsp;<a href="../../users/9854149/weiya-ou" title="2,730 reputation" class="comment-user ">weiya ou</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment125817126_822464"><span title="2022-02-18T16:59:33.317 License: CC BY-SA 4.0" class="relativetime-clean">Feb 18 '22 at 16:59</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="47140708"></a> <div id="answer-47140708" class="answer " data-answerid="47140708" data-ownerid="4136520" data-score="285" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="47140708"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="285">285</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I would like to share an edited version of the <a href="../../a/822486#822486"><strong>Shog9</strong>'s approved answer</a>.</p> <hr/> <p>As <strong>Mike Samuel</strong> pointed with a comment, that function can execute inline javascript code.<br/> But <strong>Shog9</strong> is right when saying "let the browser do it for you..."</p> <p>so.. here my edited version, using <a class="external-link" href="https://developer.mozilla.org/en-US/docs/Web/API/DOMParser" rel="noreferrer">DOMParser</a>:</p> <pre><code>function strip(html){ let doc = new DOMParser().parseFromString(html, 'text/html'); return doc.body.textContent || ""; } </code></pre> <p>here the code to test the inline javascript:</p> <pre><code>strip("&lt;img onerror='alert(\"could run arbitrary JS here\")' src=bogus&gt;") </code></pre> <p>Also, it does not request resources on parse (like images)</p> <pre><code>strip("Just text &lt;img src='https://assets.rbl.ms/4155638/980x.jpg'&gt;") </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Dec 27 '22 at 01:09">edited Dec 27 '22 at 01:09</time> <a href="../../users/11107541/starball" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/11107541.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="starball" /> </a> <div class="s-user-card--info"> <a href="../../users/11107541/starball" class="s-user-card--link">starball</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">20,030</li> <li class="s-award-bling s-award-bling__gold" title="7 gold badges">7</li> <li class="s-award-bling s-award-bling__silver" title="43 silver badges">43</li> <li class="s-award-bling s-award-bling__bronze" title="238 bronze badges">238</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 06 '17 at 15:46">answered Nov 06 '17 at 15:46</time> <a href="../../users/4136520/sabaz" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4136520.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Sabaz" /> </a> <div class="s-user-card--info"> <a href="../../users/4136520/sabaz" class="s-user-card--link">Sabaz</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">4,794</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="18 silver badges">18</li> <li class="s-award-bling s-award-bling__bronze" title="26 bronze badges">26</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-47140708" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="47140708" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-84400948" class="comment js-comment " data-comment-id="84400948" data-comment-owner-id="2972087" data-comment-score="10"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">10</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment84400948_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">It's worth to add that this solution work only in browser.</span> –&nbsp;<a href="../../users/2972087/kris-iv" title="2,396 reputation" class="comment-user ">kris_IV</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment84400948_47140708"><span title="2018-02-09T08:08:47.257 License: CC BY-SA 3.0" class="relativetime-clean">Feb 09 '18 at 08:08</span></a></span> </div> </div> </li> <li id="comment-91623799" class="comment js-comment " data-comment-id="91623799" data-comment-owner-id="536590" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment91623799_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This is not strip tags, but more like PHP htmlspecialchars(). Still useful for me.</span> –&nbsp;<a href="../../users/536590/daantje" title="2,406 reputation" class="comment-user ">Daantje</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment91623799_47140708"><span title="2018-09-14T19:38:57.133 License: CC BY-SA 4.0" class="relativetime-clean">Sep 14 '18 at 19:38</span></a></span> </div> </div> </li> <li id="comment-97964215" class="comment js-comment " data-comment-id="97964215" data-comment-owner-id="480608" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment97964215_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Note that this also removes whitespace from the beginning of the text.</span> –&nbsp;<a href="../../users/480608/raine-revere" title="30,985 reputation" class="comment-user ">Raine Revere</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment97964215_47140708"><span title="2019-04-11T15:48:15.683 License: CC BY-SA 4.0" class="relativetime-clean">Apr 11 '19 at 15:48</span></a></span> </div> </div> </li> <li id="comment-109550931" class="comment js-comment " data-comment-id="109550931" data-comment-owner-id="4188138" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment109550931_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This seems to be *much* faster than @Shog9's answer</span> –&nbsp;<a href="../../users/4188138/shmuel-kamensky" title="411 reputation" class="comment-user ">Shmuel Kamensky</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment109550931_47140708"><span title="2020-05-21T16:45:02.477 License: CC BY-SA 4.0" class="relativetime-clean">May 21 '20 at 16:45</span></a></span> </div> </div> </li> <li id="comment-116416500" class="comment js-comment " data-comment-id="116416500" data-comment-owner-id="501765" data-comment-score="2"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">2</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment116416500_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">also, it does not try to [parse html using regex](https://stackoverflow.com/a/1732454/501765)</span> –&nbsp;<a href="../../users/501765/torzsmokus" title="1,799 reputation" class="comment-user ">törzsmókus</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment116416500_47140708"><span title="2021-01-22T10:53:11.730 License: CC BY-SA 4.0" class="relativetime-clean">Jan 22 '21 at 10:53</span></a></span> </div> </div> </li> <li id="comment-122780708" class="comment js-comment " data-comment-id="122780708" data-comment-owner-id="5572406" data-comment-score="6"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">6</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment122780708_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This should be the accepted answer because it's the safest and fastest way to do</span> –&nbsp;<a href="../../users/5572406/the-previ" title="683 reputation" class="comment-user ">the_previ</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment122780708_47140708"><span title="2021-10-06T12:00:54.020 License: CC BY-SA 4.0" class="relativetime-clean">Oct 06 '21 at 12:00</span></a></span> </div> </div> </li> <li id="comment-131856867" class="comment js-comment " data-comment-id="131856867" data-comment-owner-id="1071698" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment131856867_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">What does the or in the return does? Looks like this function will return a boolean.</span> –&nbsp;<a href="../../users/1071698/ronen-festinger" title="2,260 reputation" class="comment-user ">Ronen Festinger</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment131856867_47140708"><span title="2022-12-06T20:02:46.263 License: CC BY-SA 4.0" class="relativetime-clean">Dec 06 '22 at 20:02</span></a></span> </div> </div> </li> <li id="comment-133306783" class="comment js-comment " data-comment-id="133306783" data-comment-owner-id="821285" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment133306783_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">It won't return a boolean; it will return `doc.body.textContent` if it is truthy, or `""` otherwise, i.e. the first item that is true when turned into a boolean.</span> –&nbsp;<a href="../../users/821285/ian-kim" title="170 reputation" class="comment-user ">Ian Kim</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment133306783_47140708"><span title="2023-02-24T15:19:28.577 License: CC BY-SA 4.0" class="relativetime-clean">Feb 24 '23 at 15:19</span></a></span> </div> </div> </li> <li id="comment-133574390" class="comment js-comment " data-comment-id="133574390" data-comment-owner-id="11133602" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment133574390_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This doesn't seem to strip HTML tags recursively. How can I recursively parse HTML into text? Thanks.</span> –&nbsp;<a href="../../users/11133602/teddy-c" title="786 reputation" class="comment-user ">Teddy C</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment133574390_47140708"><span title="2023-03-13T04:31:28.037 License: CC BY-SA 4.0" class="relativetime-clean">Mar 13 '23 at 04:31</span></a></span> </div> </div> </li> <li id="comment-134760764" class="comment js-comment " data-comment-id="134760764" data-comment-owner-id="15266227" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment134760764_47140708"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">We have used this, but found that it doesn't work with sth like `\"&gt;<script>document.write('<img src=//X55.is OnLoad=import(src)>');</script>`. Calling strip() recursively in case some tags still exists fixes that https://stackoverflow.com/a/76424919/15266227</span> –&nbsp;<a href="../../users/15266227/samuel-eiche" title="139 reputation" class="comment-user ">Samuel Eiche</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment134760764_47140708"><span title="2023-06-07T15:34:56.257 License: CC BY-SA 4.0" class="relativetime-clean">Jun 07 '23 at 15:34</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="8632453"></a> <div id="answer-8632453" class="answer " data-answerid="8632453" data-ownerid="1115741" data-score="278" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="8632453"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="278">278</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Simplest way:</p> <pre><code>jQuery(html).text(); </code></pre> <p>That retrieves all the text from a string of html. </p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Aug 24 '12 at 18:18">edited Aug 24 '12 at 18:18</time> <a href="../../users/-1/community" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/-1.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Community" /> </a> <div class="s-user-card--info"> <a href="../../users/-1/community" class="s-user-card--link">Community</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 26 '11 at 01:26">answered Dec 26 '11 at 01:26</time> <a href="../../users/1115741/mark" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1115741.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mark" /> </a> <div class="s-user-card--info"> <a href="../../users/1115741/mark" class="s-user-card--link">Mark</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,925</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="13 silver badge">13</li> <li class="s-award-bling s-award-bling__bronze" title="2 bronze badge">2</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-8632453" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="8632453" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-12336510" class="comment js-comment " data-comment-id="12336510" data-comment-owner-id="1115741" data-comment-score="114"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">114</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment12336510_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">We always use jQuery for projects since invariably our projects have a lot of Javascript. Therefore we didn't add bulk, we took advantage of existing API code...</span> –&nbsp;<a href="../../users/1115741/mark" title="2,925 reputation" class="comment-user ">Mark</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment12336510_8632453"><span title="2012-03-14T16:31:56.187 License: CC BY-SA 3.0" class="relativetime-clean">Mar 14 '12 at 16:31</span></a></span> </div> </div> </li> <li id="comment-12337222" class="comment js-comment " data-comment-id="12337222" data-comment-owner-id="572771" data-comment-score="40"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">40</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment12337222_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You use it, but the OP might not. the question was about Javascript NOT JQuery.</span> –&nbsp;<a href="../../users/572771/rafael-herscovici" title="16,558 reputation" class="comment-user ">Rafael Herscovici</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment12337222_8632453"><span title="2012-03-14T16:55:47.470 License: CC BY-SA 3.0" class="relativetime-clean">Mar 14 '12 at 16:55</span></a></span> </div> </div> </li> <li id="comment-17965841" class="comment js-comment " data-comment-id="17965841" data-comment-owner-id="218119" data-comment-score="2"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">2</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment17965841_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">If you are using CKEditor, you already have jQuery loading. But to get all of the actual characters for an accurate count, you need to trim the result: chars = jQuery(editor.getData()).text().trim())</span> –&nbsp;<a href="../../users/218119/benxamin" title="4,774 reputation" class="comment-user ">Benxamin</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment17965841_8632453"><span title="2012-11-02T14:30:50.780 License: CC BY-SA 3.0" class="relativetime-clean">Nov 02 '12 at 14:30</span></a></span> </div> </div> </li> <li id="comment-18673113" class="comment js-comment " data-comment-id="18673113" data-comment-owner-id="807674" data-comment-score="119"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">119</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment18673113_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">It's still a useful answer for people who need to do the same thing as the OP (like me) and don't mind using jQuery (like me), not to mention, it could have been useful to the OP if they were considering using jQuery. The point of the site is to share knowledge. Keep in mind that the chilling effect you might have by chastising useful answers without good reason.</span> –&nbsp;<a href="../../users/807674/acjay" title="34,571 reputation" class="comment-user ">acjay</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment18673113_8632453"><span title="2012-11-29T01:32:54.040 License: CC BY-SA 3.0" class="relativetime-clean">Nov 29 '12 at 01:32</span></a></span> </div> </div> </li> <li id="comment-19126115" class="comment js-comment " data-comment-id="19126115" data-comment-owner-id="553394" data-comment-score="31"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">31</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment19126115_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@Dementic shockingly, I find the threads with multiple answers to be the most useful, because often a secondary answer meets my exact needs, while the primary answer meets the general case.</span> –&nbsp;<a href="../../users/553394/eric-g" title="1,429 reputation" class="comment-user ">Eric G</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment19126115_8632453"><span title="2012-12-14T19:11:41.307 License: CC BY-SA 3.0" class="relativetime-clean">Dec 14 '12 at 19:11</span></a></span> </div> </div> </li> <li id="comment-20567259" class="comment js-comment " data-comment-id="20567259" data-comment-owner-id="569751" data-comment-score="38"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">38</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment20567259_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">That will not work if you some part of string is not wrapped in html tag. e.g. "<b>Error:</b> Please enter a valid email" will return only "Error:"</span> –&nbsp;<a href="../../users/569751/aamir-afridi" title="6,364 reputation" class="comment-user ">Aamir Afridi</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment20567259_8632453"><span title="2013-02-05T11:10:33.953 License: CC BY-SA 3.0" class="relativetime-clean">Feb 05 '13 at 11:10</span></a></span> </div> </div> </li> <li id="comment-20775423" class="comment js-comment " data-comment-id="20775423" data-comment-owner-id="676798" data-comment-score="18"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">18</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment20775423_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The comment by Mike Samuel above applies here as well. Don't use this with HTML from an untrusted source. To see why, try running `jQuery("<img onerror='alert(\"could run arbitrary JS here\")'/>").text();`</span> –&nbsp;<a href="../../users/676798/janne-aukia" title="1,003 reputation" class="comment-user ">Janne Aukia</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment20775423_8632453"><span title="2013-02-12T08:38:39.083 License: CC BY-SA 3.0" class="relativetime-clean">Feb 12 '13 at 08:38</span></a></span> </div> </div> </li> <li id="comment-21220737" class="comment js-comment " data-comment-id="21220737" data-comment-owner-id="1315125" data-comment-score="2"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">2</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment21220737_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@dementic: in the tags there is also jQuery, so I do not see why this would not be a valid answer.. +1 helped me</span> –&nbsp;<a href="../../users/1315125/igor-l" title="3,159 reputation" class="comment-user ">Igor L.</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment21220737_8632453"><span title="2013-02-26T10:48:19.287 License: CC BY-SA 3.0" class="relativetime-clean">Feb 26 '13 at 10:48</span></a></span> </div> </div> </li> <li id="comment-21221827" class="comment js-comment " data-comment-id="21221827" data-comment-owner-id="572771" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment21221827_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@IgorLacik - if you check the edits, when i wrote the comment Jquery was not in the tags, it was added because of the jquery answers.</span> –&nbsp;<a href="../../users/572771/rafael-herscovici" title="16,558 reputation" class="comment-user ">Rafael Herscovici</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment21221827_8632453"><span title="2013-02-26T11:22:04.877 License: CC BY-SA 3.0" class="relativetime-clean">Feb 26 '13 at 11:22</span></a></span> </div> </div> </li> <li id="comment-23062362" class="comment js-comment " data-comment-id="23062362" data-comment-owner-id="210578" data-comment-score="16"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">16</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment23062362_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You should wrap it in a HTML element to make it valid for text strings as well: `$('<i>').html(html).text()`. This will also work backend in node.js.</i></span> –&nbsp;<a href="../../users/210578/david-hellsing" title="106,495 reputation" class="comment-user ">David Hellsing</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment23062362_8632453"><span title="2013-04-22T08:39:00.597 License: CC BY-SA 3.0" class="relativetime-clean">Apr 22 '13 at 08:39</span></a></span> </div> </div> </li> <li id="comment-23557155" class="comment js-comment " data-comment-id="23557155" data-comment-owner-id="29182" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment23557155_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@JanneAukia I was curious so I made a fiddle. http://jsfiddle.net/gPdZm/1/ I would expect the alert to be run twice: once when the page loads, and then once more when we evaluate the untrusted JS. Is that not right? Or does the alert only fire the first time it is evaluated by anything?</span> –&nbsp;<a href="../../users/29182/ziggy" title="21,845 reputation" class="comment-user ">Ziggy</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment23557155_8632453"><span title="2013-05-07T19:31:05.910 License: CC BY-SA 3.0" class="relativetime-clean">May 07 '13 at 19:31</span></a></span> </div> </div> </li> <li id="comment-26912290" class="comment js-comment " data-comment-id="26912290" data-comment-owner-id="1218430" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment26912290_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This is not effective performance wise since it will create a dom element first which would trigger the loading of images if any. Stripping the html text suggested by @nickf is a better idea.</span> –&nbsp;<a href="../../users/1218430/agaase" title="1,562 reputation" class="comment-user ">agaase</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment26912290_8632453"><span title="2013-08-20T12:46:40.270 License: CC BY-SA 3.0" class="relativetime-clean">Aug 20 '13 at 12:46</span></a></span> </div> </div> </li> <li id="comment-29192066" class="comment js-comment " data-comment-id="29192066" data-comment-owner-id="2263584" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment29192066_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This will not turn `<br/>` into '"\r\n"', so a multiline text may be broken if you run just that code. For single line text it should be ok, though.</span> –&nbsp;<a href="../../users/2263584/geeky-guy" title="9,229 reputation" class="comment-user ">Geeky Guy</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment29192066_8632453"><span title="2013-10-29T13:01:34.590 License: CC BY-SA 3.0" class="relativetime-clean">Oct 29 '13 at 13:01</span></a></span> </div> </div> </li> <li id="comment-35948373" class="comment js-comment " data-comment-id="35948373" data-comment-owner-id="1246140" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment35948373_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I tried this with <img onload="alert('hi');"/> and the javascript still executes even though the image tags are stripped.</span> –&nbsp;<a href="../../users/1246140/nile" title="1,586 reputation" class="comment-user ">Nile</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment35948373_8632453"><span title="2014-05-04T00:42:46.343 License: CC BY-SA 3.0" class="relativetime-clean">May 04 '14 at 00:42</span></a></span> </div> </div> </li> <li id="comment-48285949" class="comment js-comment " data-comment-id="48285949" data-comment-owner-id="288671" data-comment-score="3"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">3</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment48285949_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">works with angular as well: `angular.element(html).text();` (the actual call is delegated to jquery lite)</span> –&nbsp;<a href="../../users/288671/vitalii-fedorenko" title="110,878 reputation" class="comment-user ">Vitalii Fedorenko</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment48285949_8632453"><span title="2015-05-06T19:59:25.950 License: CC BY-SA 3.0" class="relativetime-clean">May 06 '15 at 19:59</span></a></span> </div> </div> </li> <li id="comment-63406583" class="comment js-comment " data-comment-id="63406583" data-comment-owner-id="2143584" data-comment-score="3"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">3</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment63406583_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This will not work if 'html' has plain string. e.g `jQuery("<a>abc</a>").text()` will output "abc". However `jQuery("abc").text()` will output "" (expected abc)</span> –&nbsp;<a href="../../users/2143584/raja-ehtesham" title="575 reputation" class="comment-user ">Raja Ehtesham</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment63406583_8632453"><span title="2016-06-23T00:47:13.100 License: CC BY-SA 3.0" class="relativetime-clean">Jun 23 '16 at 00:47</span></a></span> </div> </div> </li> <li id="comment-66063435" class="comment js-comment " data-comment-id="66063435" data-comment-owner-id="1126008" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment66063435_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This still executes probably dangerous code `jQuery('<span>Text :) <img onerror="alert(1)"/></span>').text()`</span> –&nbsp;<a href="../../users/1126008/simon" title="594 reputation" class="comment-user ">Simon</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment66063435_8632453"><span title="2016-09-07T10:42:08.553 License: CC BY-SA 3.0" class="relativetime-clean">Sep 07 '16 at 10:42</span></a></span> </div> </div> </li> <li id="comment-66646262" class="comment js-comment " data-comment-id="66646262" data-comment-owner-id="" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment66646262_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`jQuery('<span>').html(html).text();` not `jQuery(html).text();`</span></span> –&nbsp;<a class="comment-user "></a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment66646262_8632453"><span title="2016-09-24T04:48:02.017 License: CC BY-SA 3.0" class="relativetime-clean">Sep 24 '16 at 04:48</span></a></span> </div> </div> </li> <li id="comment-81607864" class="comment js-comment " data-comment-id="81607864" data-comment-owner-id="212692" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment81607864_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">in case it's dynamic, $('some string').text() gives "" (empty string)</span> –&nbsp;<a href="../../users/212692/prashant" title="5,331 reputation" class="comment-user ">Prashant</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment81607864_8632453"><span title="2017-11-16T11:36:38.217 License: CC BY-SA 3.0" class="relativetime-clean">Nov 16 '17 at 11:36</span></a></span> </div> </div> </li> <li id="comment-95194338" class="comment js-comment " data-comment-id="95194338" data-comment-owner-id="1275966" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment95194338_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The question asks about javascript. Jquery is not javascript, jquery is a javascript library.</span> –&nbsp;<a href="../../users/1275966/ajimix" title="974 reputation" class="comment-user ">ajimix</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment95194338_8632453"><span title="2019-01-14T14:05:13.303 License: CC BY-SA 4.0" class="relativetime-clean">Jan 14 '19 at 14:05</span></a></span> </div> </div> </li> <li id="comment-134751489" class="comment js-comment " data-comment-id="134751489" data-comment-owner-id="1120027" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment134751489_8632453"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This answer was written over 10 years ago when jQuery was likely in wider use. I updated the question to more accurately reflect the original intent. That will make this answer obsolete and should be deleted. However it is historically useful and should be left as a comment.</span> –&nbsp;<a href="../../users/1120027/jgallardo" title="11,074 reputation" class="comment-user ">JGallardo</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment134751489_8632453"><span title="2023-06-06T23:55:28.507 License: CC BY-SA 4.0" class="relativetime-clean">Jun 06 '23 at 23:55</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="14337611"></a> <div id="answer-14337611" class="answer " data-answerid="14337611" data-ownerid="999305" data-score="61" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="14337611"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="61">61</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>As an extension to the jQuery method, if your string might not contain HTML (eg if you are trying to remove HTML from a form field)</p> <pre><code>jQuery(html).text(); </code></pre> <p>will return an empty string if there is no HTML</p> <p>Use:</p> <pre><code>jQuery('&lt;p&gt;' + html + '&lt;/p&gt;').text(); </code></pre> <p>instead.</p> <p><strong>Update:</strong> As has been pointed out in the comments, in some circumstances this solution will execute javascript contained within <code>html</code> if the value of <code>html</code> could be influenced by an attacker, use a different solution.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Oct 23 '20 at 14:47">edited Oct 23 '20 at 14:47</time> <a href="../../users/4684797/black" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4684797.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Black" /> </a> <div class="s-user-card--info"> <a href="../../users/4684797/black" class="s-user-card--link">Black</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">18,150</li> <li class="s-award-bling s-award-bling__gold" title="39 gold badges">39</li> <li class="s-award-bling s-award-bling__silver" title="158 silver badges">158</li> <li class="s-award-bling s-award-bling__bronze" title="271 bronze badges">271</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 15 '13 at 12:20">answered Jan 15 '13 at 12:20</time> <a href="../../users/999305/user999305" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/999305.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="user999305" /> </a> <div class="s-user-card--info"> <a href="../../users/999305/user999305" class="s-user-card--link">user999305</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,013</li> <li class="s-award-bling s-award-bling__silver" title="10 silver badges">10</li> <li class="s-award-bling s-award-bling__bronze" title="15 bronze badges">15</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-14337611" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="14337611" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-39413946" class="comment js-comment " data-comment-id="39413946" data-comment-owner-id="940072" data-comment-score="15"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">15</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment39413946_14337611"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Or `$("<p>").html(html).text();`</p></span> –&nbsp;<a href="../../users/940072/dimitar-dimitrov" title="14,868 reputation" class="comment-user ">Dimitar Dimitrov</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment39413946_14337611"><span title="2014-08-13T15:49:38.987 License: CC BY-SA 3.0" class="relativetime-clean">Aug 13 '14 at 15:49</span></a></span> </div> </div> </li> <li id="comment-66063457" class="comment js-comment " data-comment-id="66063457" data-comment-owner-id="1126008" data-comment-score="6"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">6</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment66063457_14337611"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This still executes probably dangerous code `jQuery('<span>Text :) <img onerror="alert(1)"/></span>').text()`</span> –&nbsp;<a href="../../users/1126008/simon" title="594 reputation" class="comment-user ">Simon</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment66063457_14337611"><span title="2016-09-07T10:42:43.210 License: CC BY-SA 3.0" class="relativetime-clean">Sep 07 '16 at 10:42</span></a></span> </div> </div> </li> <li id="comment-77779308" class="comment js-comment " data-comment-id="77779308" data-comment-owner-id="1522816" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment77779308_14337611"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">try jQuery("aa&lt;script&gt;alert(1)&lt;/script&gt;a").text();</span> –&nbsp;<a href="../../users/1522816/grzegorz-kaczan" title="21,186 reputation" class="comment-user ">Grzegorz Kaczan</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment77779308_14337611"><span title="2017-07-31T07:17:24.797 License: CC BY-SA 3.0" class="relativetime-clean">Jul 31 '17 at 07:17</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="1237620"></a> <div id="answer-1237620" class="answer " data-answerid="1237620" data-ownerid="151601" data-score="48" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="1237620"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="48">48</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><h2>Converting HTML for Plain Text emailing keeping hyperlinks (a href) intact</h2> <p>The above function posted by hypoxide works fine, but I was after something that would basically convert HTML created in a Web RichText editor (for example FCKEditor) and clear out all HTML but leave all the Links due the fact that I wanted both the HTML and the plain text version to aid creating the correct parts to an STMP email (both HTML and plain text).</p> <p>After a long time of searching Google myself and my collegues came up with this using the regex engine in Javascript:</p> <pre><code>str='this string has &lt;i&gt;html&lt;/i&gt; code i want to &lt;b&gt;remove&lt;/b&gt;&lt;br&gt;Link Number 1 -&gt;&lt;a href="http://www.bbc.co.uk"&gt;BBC&lt;/a&gt; Link Number 1&lt;br&gt;&lt;p&gt;Now back to normal text and stuff&lt;/p&gt; '; str=str.replace(/&lt;br&gt;/gi, "\n"); str=str.replace(/&lt;p.*&gt;/gi, "\n"); str=str.replace(/&lt;a.*href="(.*?)".*&gt;(.*?)&lt;\/a&gt;/gi, " $2 (Link-&gt;$1) "); str=str.replace(/&lt;(?:.|\s)*?&gt;/g, ""); </code></pre> <p>the <code>str</code> variable starts out like this:</p> <pre><code>this string has &lt;i&gt;html&lt;/i&gt; code i want to &lt;b&gt;remove&lt;/b&gt;&lt;br&gt;Link Number 1 -&gt;&lt;a href="http://www.bbc.co.uk"&gt;BBC&lt;/a&gt; Link Number 1&lt;br&gt;&lt;p&gt;Now back to normal text and stuff&lt;/p&gt; </code></pre> <p>and then after the code has run it looks like this:-</p> <pre><code>this string has html code i want to remove Link Number 1 -&gt; BBC (Link-&gt;http://www.bbc.co.uk) Link Number 1 Now back to normal text and stuff </code></pre> <p>As you can see the all the HTML has been removed and the Link have been persevered with the hyperlinked text is still intact. Also I have replaced the <code>&lt;p&gt;</code> and <code>&lt;br&gt;</code> tags with <code>\n</code> (newline char) so that some sort of visual formatting has been retained.</p> <p>To change the link format (eg. <code>BBC (Link-&gt;http://www.bbc.co.uk)</code> ) just edit the <code>$2 (Link-&gt;$1)</code>, where <code>$1</code> is the href URL/URI and the <code>$2</code> is the hyperlinked text. With the links directly in body of the plain text most SMTP Mail Clients convert these so the user has the ability to click on them.</p> <p>Hope you find this useful.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Jun 18 '15 at 14:21">edited Jun 18 '15 at 14:21</time> <a href="../../users/2086547/victor" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2086547.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Victor" /> </a> <div class="s-user-card--info"> <a href="../../users/2086547/victor" class="s-user-card--link">Victor</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">3,081</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="18 silver badges">18</li> <li class="s-award-bling s-award-bling__bronze" title="20 bronze badges">20</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Aug 06 '09 at 08:30">answered Aug 06 '09 at 08:30</time> <a href="../../users/151601/jibberboy2000" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/151601.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Jibberboy2000" /> </a> <div class="s-user-card--info"> <a href="../../users/151601/jibberboy2000" class="s-user-card--link">Jibberboy2000</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">524</li> <li class="s-award-bling s-award-bling__silver" title="4 silver badges">4</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-1237620" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="1237620" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-93980771" class="comment js-comment " data-comment-id="93980771" data-comment-owner-id="5798225" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment93980771_1237620"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">It doesn't handle " "</span> –&nbsp;<a href="../../users/5798225/rose-nettoyeur" title="885 reputation" class="comment-user ">Rose Nettoyeur</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment93980771_1237620"><span title="2018-11-30T12:38:26.070 License: CC BY-SA 4.0" class="relativetime-clean">Nov 30 '18 at 12:38</span></a></span> </div> </div> </li> <li id="comment-116416457" class="comment js-comment " data-comment-id="116416457" data-comment-owner-id="501765" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment116416457_1237620"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">obligatory caveat: https://stackoverflow.com/a/1732454/501765</span> –&nbsp;<a href="../../users/501765/torzsmokus" title="1,799 reputation" class="comment-user ">törzsmókus</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment116416457_1237620"><span title="2021-01-22T10:51:16.597 License: CC BY-SA 4.0" class="relativetime-clean">Jan 22 '21 at 10:51</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="17980070"></a> <div id="answer-17980070" class="answer " data-answerid="17980070" data-ownerid="2639465" data-score="36" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="17980070"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="36">36</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>An improvement to the accepted answer.</p> <pre><code>function strip(html) { var tmp = document.implementation.createHTMLDocument("New").body; tmp.innerHTML = html; return tmp.textContent || tmp.innerText || ""; } </code></pre> <p>This way something running like this will do no harm:</p> <pre><code>strip("&lt;img onerror='alert(\"could run arbitrary JS here\")' src=bogus&gt;") </code></pre> <p>Firefox, Chromium and Explorer 9+ are safe. Opera Presto is still vulnerable. Also images mentioned in the strings are not downloaded in Chromium and Firefox saving http requests.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2018-09-19T15:26:03.830" class="relativetime">Sep 19 '18 at 15:26</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Janghou</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 31 '13 at 20:14">answered Jul 31 '13 at 20:14</time> <a href="../../users/2639465/janghou" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2639465.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Janghou" /> </a> <div class="s-user-card--info"> <a href="../../users/2639465/janghou" class="s-user-card--link">Janghou</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,613</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="21 silver badge">21</li> <li class="s-award-bling s-award-bling__bronze" title="30 bronze badge">30</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-17980070" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="17980070" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-61126271" class="comment js-comment " data-comment-id="61126271" data-comment-owner-id="1051628" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment61126271_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This is some of the way there, but isn't safe from `<script><script>alert();`</script></span> –&nbsp;<a href="../../users/1051628/arth" title="12,789 reputation" class="comment-user ">Arth</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment61126271_17980070"><span title="2016-04-21T15:53:39.980 License: CC BY-SA 3.0" class="relativetime-clean">Apr 21 '16 at 15:53</span></a></span> </div> </div> </li> <li id="comment-61157499" class="comment js-comment " data-comment-id="61157499" data-comment-owner-id="2639465" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment61157499_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">That doesn't run any scripts here in Chromium/Opera/Firefox on Linux, so why isn't it safe?</span> –&nbsp;<a href="../../users/2639465/janghou" title="1,613 reputation" class="comment-user ">Janghou</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment61157499_17980070"><span title="2016-04-22T10:37:05.250 License: CC BY-SA 3.0" class="relativetime-clean">Apr 22 '16 at 10:37</span></a></span> </div> </div> </li> <li id="comment-61158327" class="comment js-comment " data-comment-id="61158327" data-comment-owner-id="1051628" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment61158327_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">My apologies, I must have miss-tested, I probably forgot to click run again on the jsFiddle.</span> –&nbsp;<a href="../../users/1051628/arth" title="12,789 reputation" class="comment-user ">Arth</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment61158327_17980070"><span title="2016-04-22T10:59:13.023 License: CC BY-SA 3.0" class="relativetime-clean">Apr 22 '16 at 10:59</span></a></span> </div> </div> </li> <li id="comment-69507537" class="comment js-comment " data-comment-id="69507537" data-comment-owner-id="12484" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment69507537_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">The "New" argument is superfluous, I think?</span> –&nbsp;<a href="../../users/12484/jon-schneider" title="25,758 reputation" class="comment-user ">Jon Schneider</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment69507537_17980070"><span title="2016-12-14T21:43:12.190 License: CC BY-SA 3.0" class="relativetime-clean">Dec 14 '16 at 21:43</span></a></span> </div> </div> </li> <li id="comment-69529124" class="comment js-comment " data-comment-id="69529124" data-comment-owner-id="2639465" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment69529124_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">According to the [specs](https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument) it's optional nowadays, but it wasn't always.</span> –&nbsp;<a href="../../users/2639465/janghou" title="1,613 reputation" class="comment-user ">Janghou</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment69529124_17980070"><span title="2016-12-15T12:38:44.983 License: CC BY-SA 3.0" class="relativetime-clean">Dec 15 '16 at 12:38</span></a></span> </div> </div> </li> <li id="comment-77779290" class="comment js-comment " data-comment-id="77779290" data-comment-owner-id="1522816" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment77779290_17980070"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">strip("aa&lt;script&gt;alert(1)&lt;/script&gt;a")</span> –&nbsp;<a href="../../users/1522816/grzegorz-kaczan" title="21,186 reputation" class="comment-user ">Grzegorz Kaczan</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment77779290_17980070"><span title="2017-07-31T07:16:52.767 License: CC BY-SA 3.0" class="relativetime-clean">Jul 31 '17 at 07:16</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="41756926"></a> <div id="answer-41756926" class="answer " data-answerid="41756926" data-ownerid="2881350" data-score="35" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="41756926"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="35">35</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>This should do the work on any Javascript environment (NodeJS included).</p> <pre class="lang-js prettyprint-override"><code> const text = ` &lt;html lang="en"&gt; &lt;head&gt; &lt;style type="text/css"&gt;*{color:red}&lt;/style&gt; &lt;script&gt;alert('hello')&lt;/script&gt; &lt;/head&gt; &lt;body&gt;&lt;b&gt;This is some text&lt;/b&gt;&lt;br/&gt;&lt;body&gt; &lt;/html&gt;`; // Remove style tags and content text.replace(/&lt;style[^&gt;]*&gt;.*&lt;\/style&gt;/g, '') // Remove script tags and content .replace(/&lt;script[^&gt;]*&gt;.*&lt;\/script&gt;/g, '') // Remove all opening, closing and orphan HTML tags .replace(/&lt;[^&gt;]+&gt;/g, '') // Remove leading spaces and repeated CR/LF .replace(/([\r\n]+ +)+/g, ''); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2023-07-06T23:18:22.407" class="relativetime">Jul 06 '23 at 23:18</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Karl.S</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 20 '17 at 05:49">answered Jan 20 '17 at 05:49</time> <a href="../../users/2881350/karl-s" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2881350.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Karl.S" /> </a> <div class="s-user-card--info"> <a href="../../users/2881350/karl-s" class="s-user-card--link">Karl.S</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,294</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="26 silver badge">26</li> <li class="s-award-bling s-award-bling__bronze" title="33 bronze badge">33</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-41756926" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="41756926" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-84304392" class="comment js-comment " data-comment-id="84304392" data-comment-owner-id="2881350" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment84304392_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@pstanton could you give a working example of your statement ?</span> –&nbsp;<a href="../../users/2881350/karl-s" title="2,294 reputation" class="comment-user ">Karl.S</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment84304392_41756926"><span title="2018-02-06T22:42:28.230 License: CC BY-SA 3.0" class="relativetime-clean">Feb 06 '18 at 22:42</span></a></span> </div> </div> </li> <li id="comment-84306188" class="comment js-comment " data-comment-id="84306188" data-comment-owner-id="145574" data-comment-score="3"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">3</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment84306188_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`<style..>* {font-family:comic-sans;}Some Text`</style..></span> –&nbsp;<a href="../../users/145574/pstanton" title="35,033 reputation" class="comment-user ">pstanton</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment84306188_41756926"><span title="2018-02-07T00:19:05.460 License: CC BY-SA 3.0" class="relativetime-clean">Feb 07 '18 at 00:19</span></a></span> </div> </div> </li> <li id="comment-103630623" class="comment js-comment " data-comment-id="103630623" data-comment-owner-id="2881350" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment103630623_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@pstanton I have fixed the code and added comments, sorry for the late response.</span> –&nbsp;<a href="../../users/2881350/karl-s" title="2,294 reputation" class="comment-user ">Karl.S</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment103630623_41756926"><span title="2019-11-01T17:50:57.937 License: CC BY-SA 4.0" class="relativetime-clean">Nov 01 '19 at 17:50</span></a></span> </div> </div> </li> <li id="comment-116416525" class="comment js-comment " data-comment-id="116416525" data-comment-owner-id="501765" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment116416525_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">please consider reading these caveats: https://stackoverflow.com/a/1732454/501765</span> –&nbsp;<a href="../../users/501765/torzsmokus" title="1,799 reputation" class="comment-user ">törzsmókus</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment116416525_41756926"><span title="2021-01-22T10:54:16.383 License: CC BY-SA 4.0" class="relativetime-clean">Jan 22 '21 at 10:54</span></a></span> </div> </div> </li> <li id="comment-133901398" class="comment js-comment " data-comment-id="133901398" data-comment-owner-id="2943403" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment133901398_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Since there are no start of string or end of string anchors, the `m` pattern modifier is pointless. Since the first two patterns have common starts and finished, perhaps consolidate them by capturing the tagname and then using a backreference for the ending tag.</span> –&nbsp;<a href="../../users/2943403/mickmackusa" title="43,625 reputation" class="comment-user ">mickmackusa</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment133901398_41756926"><span title="2023-04-03T01:20:20.460 License: CC BY-SA 4.0" class="relativetime-clean">Apr 03 '23 at 01:20</span></a></span> </div> </div> </li> <li id="comment-135110914" class="comment js-comment " data-comment-id="135110914" data-comment-owner-id="2881350" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment135110914_41756926"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@mickmackusa indeed, apart of that using a XML parser is the best way to strip out the tags you want as törzsmókus commented above.</span> –&nbsp;<a href="../../users/2881350/karl-s" title="2,294 reputation" class="comment-user ">Karl.S</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment135110914_41756926"><span title="2023-07-06T23:20:17.997 License: CC BY-SA 4.0" class="relativetime-clean">Jul 06 '23 at 23:20</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="51208595"></a> <div id="answer-51208595" class="answer " data-answerid="51208595" data-ownerid="113083" data-score="20" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="51208595"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="20">20</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>var text = html.replace(/&lt;\/?("[^"]*"|'[^']*'|[^&gt;])*(&gt;|$)/g, ""); </code></pre> <p>This is a regex version, which is more resilient to malformed HTML, like:</p> <p><strong>Unclosed tags</strong></p> <p><code>Some text &lt;img</code></p> <p><strong>"&lt;", "&gt;" inside tag attributes</strong></p> <p><code>Some text &lt;img alt="x &gt; y"&gt;</code></p> <p><strong>Newlines</strong></p> <p><code>Some &lt;a href="http://google.com"&gt;</code></p> <p>The code</p> <pre><code>var html = '&lt;br&gt;This &lt;img alt="a&gt;b" \r\n src="a_b.gif" /&gt;is &gt; \nmy&lt;&gt;&lt; &gt; &lt;a&gt;"text"&lt;/a' var text = html.replace(/&lt;\/?("[^"]*"|'[^']*'|[^&gt;])*(&gt;|$)/g, ""); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 06 '18 at 10:39">answered Jul 06 '18 at 10:39</time> <a href="../../users/113083/hegemon" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/113083.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="hegemon" /> </a> <div class="s-user-card--info"> <a href="../../users/113083/hegemon" class="s-user-card--link">hegemon</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">6,614</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="32 silver badges">32</li> <li class="s-award-bling s-award-bling__bronze" title="30 bronze badges">30</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-51208595" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="51208595" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-122085648" class="comment js-comment " data-comment-id="122085648" data-comment-owner-id="539883" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment122085648_51208595"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">How could you flip this to do literally the opposite? I want to use `string.replace()` on ONLY the text part, and leave any HTML tags and their attributes unchanged.</span> –&nbsp;<a href="../../users/539883/ade" title="2,961 reputation" class="comment-user ">Ade</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment122085648_51208595"><span title="2021-09-06T15:20:36.897 License: CC BY-SA 4.0" class="relativetime-clean">Sep 06 '21 at 15:20</span></a></span> </div> </div> </li> <li id="comment-125115757" class="comment js-comment " data-comment-id="125115757" data-comment-owner-id="2878835" data-comment-score="2"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">2</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment125115757_51208595"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">My personal favourite, I would also add to remove newlines like: `const deTagged = myString.replace(/&lt;\/?("[^"]*"|'[^']*'|[^&gt;])*(&gt;|$)/g, ''); const deNewlined = deTagged.replace(/\n/g, '');`</span> –&nbsp;<a href="../../users/2878835/leigh-mathieson" title="1,658 reputation" class="comment-user ">Leigh Mathieson</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment125115757_51208595"><span title="2022-01-19T15:07:41.073 License: CC BY-SA 4.0" class="relativetime-clean">Jan 19 '22 at 15:07</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="8803657"></a> <div id="answer-8803657" class="answer " data-answerid="8803657" data-ownerid="876375" data-score="19" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="8803657"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="19">19</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I altered <a href="../../a/1237620#1237620">Jibberboy2000's answer</a> to include several <code>&lt;BR /&gt;</code> tag formats, remove everything inside <code>&lt;SCRIPT&gt;</code> and <code>&lt;STYLE&gt;</code> tags, format the resulting HTML by removing multiple line breaks and spaces and convert some HTML-encoded code into normal. After some testing it appears that you can convert most of full web pages into simple text where page title and content are retained.</p> <p>In the simple example,</p> <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; &lt;html&gt; &lt;!--comment--&gt; &lt;head&gt; &lt;title&gt;This is my title&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt; &lt;style&gt; body {margin-top: 15px;} a { color: #D80C1F; font-weight:bold; text-decoration:none; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;center&gt; This string has &lt;i&gt;html&lt;/i&gt; code i want to &lt;b&gt;remove&lt;/b&gt;&lt;br&gt; In this line &lt;a href="http://www.bbc.co.uk"&gt;BBC&lt;/a&gt; with link is mentioned.&lt;br/&gt;Now back to &amp;quot;normal text&amp;quot; and stuff using &amp;lt;html encoding&amp;gt; &lt;/center&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>becomes</p> <blockquote> <p>This is my title</p> <p>This string has html code i want to remove</p> <p>In this line BBC (<a class="external-link" href="http://www.bbc.co.uk" rel="noreferrer">http://www.bbc.co.uk</a>) with link is mentioned.</p> <p>Now back to "normal text" and stuff using </p> </blockquote> <p>The JavaScript function and test page look this:</p> <pre><code>function convertHtmlToText() { var inputText = document.getElementById("input").value; var returnText = "" + inputText; //-- remove BR tags and replace them with line break returnText=returnText.replace(/&lt;br&gt;/gi, "\n"); returnText=returnText.replace(/&lt;br\s\/&gt;/gi, "\n"); returnText=returnText.replace(/&lt;br\/&gt;/gi, "\n"); //-- remove P and A tags but preserve what's inside of them returnText=returnText.replace(/&lt;p.*&gt;/gi, "\n"); returnText=returnText.replace(/&lt;a.*href="(.*?)".*&gt;(.*?)&lt;\/a&gt;/gi, " $2 ($1)"); //-- remove all inside SCRIPT and STYLE tags returnText=returnText.replace(/&lt;script.*&gt;[\w\W]{1,}(.*?)[\w\W]{1,}&lt;\/script&gt;/gi, ""); returnText=returnText.replace(/&lt;style.*&gt;[\w\W]{1,}(.*?)[\w\W]{1,}&lt;\/style&gt;/gi, ""); //-- remove all else returnText=returnText.replace(/&lt;(?:.|\s)*?&gt;/g, ""); //-- get rid of more than 2 multiple line breaks: returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n"); //-- get rid of more than 2 spaces: returnText = returnText.replace(/ +(?= )/g,''); //-- get rid of html-encoded characters: returnText=returnText.replace(/&amp;nbsp;/gi," "); returnText=returnText.replace(/&amp;amp;/gi,"&amp;"); returnText=returnText.replace(/&amp;quot;/gi,'"'); returnText=returnText.replace(/&amp;lt;/gi,'&lt;'); returnText=returnText.replace(/&amp;gt;/gi,'&gt;'); //-- return document.getElementById("output").value = returnText; } </code></pre> <p>It was used with this HTML:</p> <pre><code>&lt;textarea id="input" style="width: 400px; height: 300px;"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;button onclick="convertHtmlToText()"&gt;CONVERT&lt;/button&gt;&lt;br /&gt; &lt;textarea id="output" style="width: 400px; height: 300px;"&gt;&lt;/textarea&gt;&lt;br /&gt; </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited May 23 '17 at 11:54">edited May 23 '17 at 11:54</time> <a href="../../users/-1/community" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/-1.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Community" /> </a> <div class="s-user-card--info"> <a href="../../users/-1/community" class="s-user-card--link">Community</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 10 '12 at 12:59">answered Jan 10 '12 at 12:59</time> <a href="../../users/876375/lenka-pitonakova" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/876375.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Lenka Pitonakova" /> </a> <div class="s-user-card--info"> <a href="../../users/876375/lenka-pitonakova" class="s-user-card--link">Lenka Pitonakova</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">979</li> <li class="s-award-bling s-award-bling__silver" title="12 silver badges">12</li> <li class="s-award-bling s-award-bling__bronze" title="14 bronze badges">14</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-8803657" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="8803657" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-17530356" class="comment js-comment " data-comment-id="17530356" data-comment-owner-id="93074" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment17530356_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I like this solution because it has treatment of html special characters... but still not nearly enough of them... the best answer for me would deal with all of them. (which is probably what jquery does).</span> –&nbsp;<a href="../../users/93074/daniel-gerson" title="2,159 reputation" class="comment-user ">Daniel Gerson</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment17530356_8803657"><span title="2012-10-17T13:17:36.080 License: CC BY-SA 3.0" class="relativetime-clean">Oct 17 '12 at 13:17</span></a></span> </div> </div> </li> <li id="comment-48199796" class="comment js-comment " data-comment-id="48199796" data-comment-owner-id="552036" data-comment-score="2"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">2</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment48199796_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I think `/<p.>/gi` should be `/<p.>/gi`.</p.></p.></span> –&nbsp;<a href="../../users/552036/cbron" title="4,036 reputation" class="comment-user ">cbron</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment48199796_8803657"><span title="2015-05-05T00:00:02.107 License: CC BY-SA 3.0" class="relativetime-clean">May 05 '15 at 00:00</span></a></span> </div> </div> </li> <li id="comment-57311233" class="comment js-comment " data-comment-id="57311233" data-comment-owner-id="212378" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57311233_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Note that to remove all `<br/>` tags you could use a good regular expression instead: `/<br/>/` that way you have just one replace instead of 3. Also it seems to me that except for the decoding of entities you can have a single regex, something like this: `/&lt;[a-z].*?\/?&gt;/`.</span> –&nbsp;<a href="../../users/212378/alexis-wilke" title="19,179 reputation" class="comment-user ">Alexis Wilke</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57311233_8803657"><span title="2016-01-14T07:11:15.530 License: CC BY-SA 3.0" class="relativetime-clean">Jan 14 '16 at 07:11</span></a></span> </div> </div> </li> <li id="comment-78384408" class="comment js-comment " data-comment-id="78384408" data-comment-owner-id="3154953" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment78384408_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Nice script. But what about table content? Any idea how can it be displayed</span> –&nbsp;<a href="../../users/3154953/hristo-enev" title="2,421 reputation" class="comment-user ">Hristo Enev</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment78384408_8803657"><span title="2017-08-16T12:14:30.260 License: CC BY-SA 3.0" class="relativetime-clean">Aug 16 '17 at 12:14</span></a></span> </div> </div> </li> <li id="comment-101848304" class="comment js-comment " data-comment-id="101848304" data-comment-owner-id="1366033" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment101848304_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">@DanielGerson, encoding html gets real hairy, real quick, but the [best approach seems to be using the he library](https://stackoverflow.com/a/57702435/1366033)</span> –&nbsp;<a href="../../users/1366033/kylemit" title="30,350 reputation" class="comment-user ">KyleMit</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment101848304_8803657"><span title="2019-08-29T03:04:21.820 License: CC BY-SA 4.0" class="relativetime-clean">Aug 29 '19 at 03:04</span></a></span> </div> </div> </li> <li id="comment-110882493" class="comment js-comment " data-comment-id="110882493" data-comment-owner-id="10643206" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment110882493_8803657"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">this fuction has a lot if itinerations which might lead to a memory leak in multiple instances of long texts.</span> –&nbsp;<a href="../../users/10643206/matias-fork" title="83 reputation" class="comment-user ">Matías Fork</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment110882493_8803657"><span title="2020-07-02T17:15:56.637 License: CC BY-SA 4.0" class="relativetime-clean">Jul 02 '20 at 17:15</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="63727322"></a> <div id="answer-63727322" class="answer " data-answerid="63727322" data-ownerid="11804213" data-score="13" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="63727322"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="13">13</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>from CSS tricks:</p> <p><a class="external-link" href="https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/" rel="noreferrer">https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/</a></p> <p></p><div class="snippet" data-babel="false" data-console="true" data-hide="false" data-lang="js"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>const originalString = ` &lt;div&gt; &lt;p&gt;Hey that's &lt;span&gt;somthing&lt;/span&gt;&lt;/p&gt; &lt;/div&gt; `; const strippedString = originalString.replace(/(&lt;([^&gt;]+)&gt;)/gi, ""); console.log(strippedString);</code></pre> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2020-10-27T06:03:14.040" class="relativetime">Oct 27 '20 at 06:03</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Anatol</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Sep 03 '20 at 15:52">answered Sep 03 '20 at 15:52</time> <a href="../../users/11804213/anatol" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/11804213.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Anatol" /> </a> <div class="s-user-card--info"> <a href="../../users/11804213/anatol" class="s-user-card--link">Anatol</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">3,720</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="20 silver badges">20</li> <li class="s-award-bling s-award-bling__bronze" title="40 bronze badges">40</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-63727322" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="63727322" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-122393692" class="comment js-comment " data-comment-id="122393692" data-comment-owner-id="11329518" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment122393692_63727322"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This fails to remove what is inside <script> and <style> tags but otherwise it is the cleanest solution.</script></span> –&nbsp;<a href="../../users/11329518/guillaume-f" title="1,010 reputation" class="comment-user ">Guillaume F.</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment122393692_63727322"><span title="2021-09-20T00:09:23.233 License: CC BY-SA 4.0" class="relativetime-clean">Sep 20 '21 at 00:09</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="822573"></a> <div id="answer-822573" class="answer " data-answerid="822573" data-ownerid="51242" data-score="8" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="822573"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="8">8</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Another, admittedly less elegant solution than nickf's or Shog9's, would be to recursively walk the DOM starting at the &lt;body&gt; tag and append each text node.</p> <pre><code>var bodyContent = document.getElementsByTagName('body')[0]; var result = appendTextNodes(bodyContent); function appendTextNodes(element) { var text = ''; // Loop through the childNodes of the passed in element for (var i = 0, len = element.childNodes.length; i &lt; len; i++) { // Get a reference to the current child var node = element.childNodes[i]; // Append the node's value if it's a text node if (node.nodeType == 3) { text += node.nodeValue; } // Recurse through the node's children, if there are any if (node.childNodes.length &gt; 0) { appendTextNodes(node); } } // Return the final result return text; } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered May 04 '09 at 23:14">answered May 04 '09 at 23:14</time> <a href="../../users/51242/bryan" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/51242.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Bryan" /> </a> <div class="s-user-card--info"> <a href="../../users/51242/bryan" class="s-user-card--link">Bryan</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,870</li> <li class="s-award-bling s-award-bling__gold" title="24 gold badges">24</li> <li class="s-award-bling s-award-bling__silver" title="39 silver badges">39</li> <li class="s-award-bling s-award-bling__bronze" title="44 bronze badges">44</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-822573" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="822573" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-631569" class="comment js-comment " data-comment-id="631569" data-comment-owner-id="9021" data-comment-score="3"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">3</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment631569_822573"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">yikes. if you're going to create a DOM tree out of your string, then just use shog's way!</span> –&nbsp;<a href="../../users/9021/nickf" title="537,072 reputation" class="comment-user ">nickf</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment631569_822573"><span title="2009-05-04T23:21:26.800 License: CC BY-SA 2.5" class="relativetime-clean">May 04 '09 at 23:21</span></a></span> </div> </div> </li> <li id="comment-631717" class="comment js-comment " data-comment-id="631717" data-comment-owner-id="51242" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment631717_822573"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Yes, my solution wields a sledge-hammer where a regular hammer is more appropriate :-). And I agree that yours and Shog9's solutions are better, and basically said as much in the answer. I also failed to reflect in my response that the html is already contained in a string, rendering my answer essentially useless as regards the original question anyway. :-(</span> –&nbsp;<a href="../../users/51242/bryan" title="2,870 reputation" class="comment-user ">Bryan</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment631717_822573"><span title="2009-05-05T00:08:42.337 License: CC BY-SA 2.5" class="relativetime-clean">May 05 '09 at 00:08</span></a></span> </div> </div> </li> <li id="comment-632354" class="comment js-comment " data-comment-id="632354" data-comment-owner-id="811" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment632354_822573"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">To be fair, this has value - if you absolutely must preserve /all/ of the text, then this has at least a decent shot at capturing newlines, tabs, carriage returns, etc... Then again, nickf's solution should do the same, and do much faster... eh.</span> –&nbsp;<a href="../../users/811/shog9" title="156,901 reputation" class="comment-user ">Shog9</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment632354_822573"><span title="2009-05-05T04:58:56.740 License: CC BY-SA 2.5" class="relativetime-clean">May 05 '09 at 04:58</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="38761241"></a> <div id="answer-38761241" class="answer " data-answerid="38761241" data-ownerid="1311367" data-score="7" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="38761241"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="7">7</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>If you want to keep the links and the structure of the content (h1, h2, etc) then you should check out <a class="external-link" href="http://textversionjs.com/" rel="noreferrer">TextVersionJS</a> You can use it with any HTML, although it was created to convert an HTML email to plain text.</p> <p>The usage is very simple. For example in node.js:</p> <pre><code>var createTextVersion = require("textversionjs"); var yourHtml = "&lt;h1&gt;Your HTML&lt;/h1&gt;&lt;ul&gt;&lt;li&gt;goes&lt;/li&gt;&lt;li&gt;here.&lt;/li&gt;&lt;/ul&gt;"; var textVersion = createTextVersion(yourHtml); </code></pre> <p>Or in the browser with pure js:</p> <pre><code>&lt;script src="textversion.js"&gt;&lt;/script&gt; &lt;script&gt; var yourHtml = "&lt;h1&gt;Your HTML&lt;/h1&gt;&lt;ul&gt;&lt;li&gt;goes&lt;/li&gt;&lt;li&gt;here.&lt;/li&gt;&lt;/ul&gt;"; var textVersion = createTextVersion(yourHtml); &lt;/script&gt; </code></pre> <p>It also works with require.js:</p> <pre><code>define(["textversionjs"], function(createTextVersion) { var yourHtml = "&lt;h1&gt;Your HTML&lt;/h1&gt;&lt;ul&gt;&lt;li&gt;goes&lt;/li&gt;&lt;li&gt;here.&lt;/li&gt;&lt;/ul&gt;"; var textVersion = createTextVersion(yourHtml); }); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Aug 04 '16 at 07:38">answered Aug 04 '16 at 07:38</time> <a href="../../users/1311367/gyula-nemeth" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1311367.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="gyula.nemeth" /> </a> <div class="s-user-card--info"> <a href="../../users/1311367/gyula-nemeth" class="s-user-card--link">gyula.nemeth</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">847</li> <li class="s-award-bling s-award-bling__silver" title="10 silver badges">10</li> <li class="s-award-bling s-award-bling__bronze" title="9 bronze badges">9</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="72976084"></a> <div id="answer-72976084" class="answer " data-answerid="72976084" data-ownerid="13625459" data-score="7" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="72976084"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="7">7</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p></p><div class="snippet" data-babel="false" data-console="true" data-hide="false" data-lang="js"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>const htmlParser= new DOMParser().parseFromString("&lt;h6&gt;User&lt;p&gt;name&lt;/p&gt;&lt;/h6&gt;" , 'text/html'); const textString= htmlParser.body.textContent; console.log(textString)</code></pre> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 14 '22 at 06:25">answered Jul 14 '22 at 06:25</time> <a href="../../users/13625459/ankit-kumawat" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/13625459.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Ankit Kumawat" /> </a> <div class="s-user-card--info"> <a href="../../users/13625459/ankit-kumawat" class="s-user-card--link">Ankit Kumawat</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">371</li> <li class="s-award-bling s-award-bling__silver" title="5 silver badges">5</li> <li class="s-award-bling s-award-bling__bronze" title="13 bronze badges">13</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-72976084" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="72976084" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-132406673" class="comment js-comment " data-comment-id="132406673" data-comment-owner-id="12534871" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment132406673_72976084"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">doesn't work in next js as it is server side rendered but nice solution for traditional applications. use this instead - const strippedString = originalString.replace(/(&lt;([^&gt;]+)&gt;)/gi, "");</span> –&nbsp;<a href="../../users/12534871/pawan-deore" title="162 reputation" class="comment-user ">Pawan Deore</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment132406673_72976084"><span title="2023-01-06T12:51:09.000 License: CC BY-SA 4.0" class="relativetime-clean">Jan 06 '23 at 12:51</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="34517493"></a> <div id="answer-34517493" class="answer " data-answerid="34517493" data-ownerid="171933" data-score="6" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="34517493"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="6">6</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>It is also possible to use the fantastic <a class="external-link" href="https://github.com/fb55/htmlparser2" rel="nofollow noreferrer">htmlparser2</a> pure JS HTML parser. Here is a working demo:</p> <pre><code>var htmlparser = require('htmlparser2'); var body = '&lt;p&gt;&lt;div&gt;This is &lt;/div&gt;a &lt;span&gt;simple &lt;/span&gt; &lt;img src="test"&gt;&lt;/img&gt;example.&lt;/p&gt;'; var result = []; var parser = new htmlparser.Parser({ ontext: function(text){ result.push(text); } }, {decodeEntities: true}); parser.write(body); parser.end(); result.join(''); </code></pre> <p>The output will be <code>This is a simple example.</code></p> <p>See it in action here: <a class="external-link" href="https://tonicdev.com/jfahrenkrug/extract-text-from-html" rel="nofollow noreferrer">https://tonicdev.com/jfahrenkrug/extract-text-from-html</a></p> <p>This works in both node and the browser if you pack your web application using a tool like webpack.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2021-11-09T14:26:55.790" class="relativetime">Nov 09 '21 at 14:26</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Johannes Fahrenkrug</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 29 '15 at 19:11">answered Dec 29 '15 at 19:11</time> <a href="../../users/171933/johannes-fahrenkrug" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/171933.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Johannes Fahrenkrug" /> </a> <div class="s-user-card--info"> <a href="../../users/171933/johannes-fahrenkrug" class="s-user-card--link">Johannes Fahrenkrug</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">42,912</li> <li class="s-award-bling s-award-bling__gold" title="19 gold badges">19</li> <li class="s-award-bling s-award-bling__silver" title="126 silver badges">126</li> <li class="s-award-bling s-award-bling__bronze" title="165 bronze badges">165</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="61081242"></a> <div id="answer-61081242" class="answer " data-answerid="61081242" data-ownerid="5105277" data-score="6" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="61081242"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="6">6</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>For easier solution, try this =&gt; <a class="external-link" href="https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/" rel="noreferrer">https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/</a></p> <pre><code>var StrippedString = OriginalString.replace(/(&lt;([^&gt;]+)&gt;)/ig,""); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 07 '20 at 13:33">answered Apr 07 '20 at 13:33</time> <a href="../../users/5105277/akshaybandivadekar" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/5105277.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="AkshayBandivadekar" /> </a> <div class="s-user-card--info"> <a href="../../users/5105277/akshaybandivadekar" class="s-user-card--link">AkshayBandivadekar</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">839</li> <li class="s-award-bling s-award-bling__silver" title="10 silver badges">10</li> <li class="s-award-bling s-award-bling__bronze" title="18 bronze badges">18</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-61081242" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="61081242" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-133901420" class="comment js-comment " data-comment-id="133901420" data-comment-owner-id="2943403" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment133901420_61081242"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Which characters in your pattern are made case-insensitive by that `i` pattern modifier? I see no need for capturing parentheses -- anywhere in the pattern. Bad copy-pasta? Maybe someone should whisper to Chris Coyier.</span> –&nbsp;<a href="../../users/2943403/mickmackusa" title="43,625 reputation" class="comment-user ">mickmackusa</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment133901420_61081242"><span title="2023-04-03T01:24:27.083 License: CC BY-SA 4.0" class="relativetime-clean">Apr 03 '23 at 01:24</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="41888402"></a> <div id="answer-41888402" class="answer " data-answerid="41888402" data-ownerid="3371679" data-score="5" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="41888402"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="5">5</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>A lot of people have answered this already, but I thought it might be useful to share the function I wrote that strips HTML tags from a string but allows you to include an array of tags that you do not want stripped. It's pretty short and has been working nicely for me.</p> <pre><code>function removeTags(string, array){ return array ? string.split("&lt;").filter(function(val){ return f(array, val); }).map(function(val){ return f(array, val); }).join("") : string.split("&lt;").map(function(d){ return d.split("&gt;").pop(); }).join(""); function f(array, value){ return array.map(function(d){ return value.includes(d + "&gt;"); }).indexOf(true) != -1 ? "&lt;" + value : value.split("&gt;")[1]; } } var x = "&lt;span&gt;&lt;i&gt;Hello&lt;/i&gt; &lt;b&gt;world&lt;/b&gt;!&lt;/span&gt;"; console.log(removeTags(x)); // Hello world! console.log(removeTags(x, ["span", "i"])); // &lt;span&gt;&lt;i&gt;Hello&lt;/i&gt; world!&lt;/span&gt; </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 27 '17 at 06:55">answered Jan 27 '17 at 06:55</time> <a href="../../users/3371679/harry-stevens" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/3371679.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Harry Stevens" /> </a> <div class="s-user-card--info"> <a href="../../users/3371679/harry-stevens" class="s-user-card--link">Harry Stevens</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,373</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="15 silver badge">15</li> <li class="s-award-bling s-award-bling__bronze" title="18 bronze badge">18</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="30792266"></a> <div id="answer-30792266" class="answer " data-answerid="30792266" data-ownerid="1531255" data-score="4" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="30792266"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="4">4</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>After trying all of the answers mentioned most if not all of them had edge cases and couldn't completely support my needs.</p> <p>I started exploring how php does it and came across the php.js lib which replicates the strip_tags method here: <a class="external-link" href="http://phpjs.org/functions/strip_tags/" rel="nofollow">http://phpjs.org/functions/strip_tags/</a></p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 11 '15 at 22:06">answered Jun 11 '15 at 22:06</time> <a href="../../users/1531255/deminetix" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1531255.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Deminetix" /> </a> <div class="s-user-card--info"> <a href="../../users/1531255/deminetix" class="s-user-card--link">Deminetix</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,866</li> <li class="s-award-bling s-award-bling__silver" title="26 silver badges">26</li> <li class="s-award-bling s-award-bling__bronze" title="21 bronze badges">21</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-30792266" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="30792266" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-57312922" class="comment js-comment " data-comment-id="57312922" data-comment-owner-id="212378" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57312922_30792266"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This is a neat function and well documented. However, it can be made faster when `allowed == ''` which I think is what the OP asked for, which is nearly what Byron answered below (Byron only got the `[^&gt;]` wrong.)</span> –&nbsp;<a href="../../users/212378/alexis-wilke" title="19,179 reputation" class="comment-user ">Alexis Wilke</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57312922_30792266"><span title="2016-01-14T08:08:53.713 License: CC BY-SA 3.0" class="relativetime-clean">Jan 14 '16 at 08:08</span></a></span> </div> </div> </li> <li id="comment-58726478" class="comment js-comment " data-comment-id="58726478" data-comment-owner-id="407245" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment58726478_30792266"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">If you use the `allowed` param you are vulnerable to XSS: `stripTags('<p onclick="alert(1)">mytext</p>', '<p>')` returns `</p><p onclick="alert(1)">mytext</p>`</span> –&nbsp;<a href="../../users/407245/chris-cinelli" title="4,679 reputation" class="comment-user ">Chris Cinelli</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment58726478_30792266"><span title="2016-02-20T01:26:58.817 License: CC BY-SA 3.0" class="relativetime-clean">Feb 20 '16 at 01:26</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="34517116"></a> <div id="answer-34517116" class="answer " data-answerid="34517116" data-ownerid="3444112" data-score="4" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="34517116"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="4">4</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>function stripHTML(my_string){ var charArr = my_string.split(''), resultArr = [], htmlZone = 0, quoteZone = 0; for( x=0; x &lt; charArr.length; x++ ){ switch( charArr[x] + htmlZone + quoteZone ){ case "&lt;00" : htmlZone = 1;break; case "&gt;10" : htmlZone = 0;resultArr.push(' ');break; case '"10' : quoteZone = 1;break; case "'10" : quoteZone = 2;break; case '"11' : case "'12" : quoteZone = 0;break; default : if(!htmlZone){ resultArr.push(charArr[x]); } } } return resultArr.join(''); } </code></pre> <p>Accounts for &gt; inside attributes and <code>&lt;img onerror="javascript"&gt;</code> in newly created dom elements.</p> <p>usage:</p> <pre><code>clean_string = stripHTML("string with &lt;html&gt; in it") </code></pre> <p>demo:</p> <p><a class="external-link" href="https://jsfiddle.net/gaby_de_wilde/pqayphzd/" rel="nofollow">https://jsfiddle.net/gaby_de_wilde/pqayphzd/</a></p> <p>demo of top answer doing the terrible things:</p> <p><a class="external-link" href="https://jsfiddle.net/gaby_de_wilde/6f0jymL6/1/" rel="nofollow">https://jsfiddle.net/gaby_de_wilde/6f0jymL6/1/</a></p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Mar 27 '16 at 07:29">edited Mar 27 '16 at 07:29</time> <a href="../../users/2401804/r3wt" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2401804.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="r3wt" /> </a> <div class="s-user-card--info"> <a href="../../users/2401804/r3wt" class="s-user-card--link">r3wt</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">4,642</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="33 silver badges">33</li> <li class="s-award-bling s-award-bling__bronze" title="55 bronze badges">55</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 29 '15 at 18:46">answered Dec 29 '15 at 18:46</time> <a href="../../users/3444112/user40521" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/3444112.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="user40521" /> </a> <div class="s-user-card--info"> <a href="../../users/3444112/user40521" class="s-user-card--link">user40521</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,997</li> <li class="s-award-bling s-award-bling__silver" title="20 silver badges">20</li> <li class="s-award-bling s-award-bling__bronze" title="8 bronze badges">8</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-34517116" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="34517116" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-80832854" class="comment js-comment " data-comment-id="80832854" data-comment-owner-id="1685098" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment80832854_34517116"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You'll need to handle escaped quotes inside an attribute value too (e.g. `string with <a malicious="attribute \">this text should be removed, but is not"&gt;example</a>`).</span> –&nbsp;<a href="../../users/1685098/logan-pickup" title="2,294 reputation" class="comment-user ">Logan Pickup</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment80832854_34517116"><span title="2017-10-25T22:00:19.240 License: CC BY-SA 3.0" class="relativetime-clean">Oct 25 '17 at 22:00</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="7649119"></a> <div id="answer-7649119" class="answer " data-answerid="7649119" data-ownerid="978602" data-score="4" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="7649119"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="4">4</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I made some modifications to original Jibberboy2000 script Hope it'll be usefull for someone</p> <pre><code>str = '**ANY HTML CONTENT HERE**'; str=str.replace(/&lt;\s*br\/*&gt;/gi, "\n"); str=str.replace(/&lt;\s*a.*href="(.*?)".*&gt;(.*?)&lt;\/a&gt;/gi, " $2 (Link-&gt;$1) "); str=str.replace(/&lt;\s*\/*.+?&gt;/ig, "\n"); str=str.replace(/ {2,}/gi, " "); str=str.replace(/\n+\s*/gi, "\n\n"); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Oct 04 '11 at 14:02">answered Oct 04 '11 at 14:02</time> <a href="../../users/978602/jaxolotl" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/978602.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Jaxolotl" /> </a> <div class="s-user-card--info"> <a href="../../users/978602/jaxolotl" class="s-user-card--link">Jaxolotl</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">49</li> <li class="s-award-bling s-award-bling__bronze" title="1 bronze badges">1</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="11460265"></a> <div id="answer-11460265" class="answer " data-answerid="11460265" data-ownerid="394433" data-score="3" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="11460265"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="3">3</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Here's a version which sorta addresses @MikeSamuel's security concern: </p> <pre><code>function strip(html) { try { var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null); doc.documentElement.innerHTML = html; return doc.documentElement.textContent||doc.documentElement.innerText; } catch(e) { return ""; } } </code></pre> <p>Note, it will return an empty string if the HTML markup isn't valid XML (aka, tags must be closed and attributes must be quoted). This isn't ideal, but does avoid the issue of having the security exploit potential.</p> <p>If not having valid XML markup is a requirement for you, you could try using:</p> <pre><code>var doc = document.implementation.createHTMLDocument(""); </code></pre> <p>but that isn't a perfect solution either for other reasons.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2012-07-12T21:10:24.877" class="relativetime">Jul 12 '12 at 21:10</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Jeremy Johnstone</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 12 '12 at 20:38">answered Jul 12 '12 at 20:38</time> <a href="../../users/394433/jeremy-johnstone" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/394433.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Jeremy Johnstone" /> </a> <div class="s-user-card--info"> <a href="../../users/394433/jeremy-johnstone" class="s-user-card--link">Jeremy Johnstone</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">348</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-11460265" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="11460265" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-57311279" class="comment js-comment " data-comment-id="57311279" data-comment-owner-id="212378" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57311279_11460265"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">That will fail in many circumstances if the text comes from user input (textarea or contenteditable widget...)</span> –&nbsp;<a href="../../users/212378/alexis-wilke" title="19,179 reputation" class="comment-user ">Alexis Wilke</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57311279_11460265"><span title="2016-01-14T07:12:39.090 License: CC BY-SA 3.0" class="relativetime-clean">Jan 14 '16 at 07:12</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="18318374"></a> <div id="answer-18318374" class="answer " data-answerid="18318374" data-ownerid="2663879" data-score="3" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="18318374"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="3">3</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I just needed to strip out the <code>&lt;a&gt;</code> tags and replace them with the text of the link.</p> <p>This seems to work great.</p> <pre><code>htmlContent= htmlContent.replace(/&lt;a.*href="(.*?)"&gt;/g, ''); htmlContent= htmlContent.replace(/&lt;\/a&gt;/g, ''); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2016-01-06T18:57:29.437" class="relativetime">Jan 06 '16 at 18:57</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">FrigginGlorious</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Aug 19 '13 at 16:12">answered Aug 19 '13 at 16:12</time> <a href="../../users/2663879/frigginglorious" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2663879.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="FrigginGlorious" /> </a> <div class="s-user-card--info"> <a href="../../users/2663879/frigginglorious" class="s-user-card--link">FrigginGlorious</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">109</li> <li class="s-award-bling s-award-bling__silver" title="2 silver badges">2</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-18318374" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="18318374" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-57008154" class="comment js-comment " data-comment-id="57008154" data-comment-owner-id="2480481" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57008154_18318374"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">This only applies for a tags and needs tweaking for being a wide function.</span> –&nbsp;<a href="../../users/2480481/m3nda" title="1,986 reputation" class="comment-user ">m3nda</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57008154_18318374"><span title="2016-01-06T11:03:26.840 License: CC BY-SA 3.0" class="relativetime-clean">Jan 06 '16 at 11:03</span></a></span> </div> </div> </li> <li id="comment-57312638" class="comment js-comment " data-comment-id="57312638" data-comment-owner-id="212378" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57312638_18318374"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Yeah, plus an anchor tag could have many other attributes such as the `title="..."`.</span> –&nbsp;<a href="../../users/212378/alexis-wilke" title="19,179 reputation" class="comment-user ">Alexis Wilke</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57312638_18318374"><span title="2016-01-14T07:58:54.727 License: CC BY-SA 3.0" class="relativetime-clean">Jan 14 '16 at 07:58</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="31407272"></a> <div id="answer-31407272" class="answer " data-answerid="31407272" data-ownerid="406659" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="31407272"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Below code allows you to retain some html tags while stripping all others </p> <pre><code>function strip_tags(input, allowed) { allowed = (((allowed || '') + '') .toLowerCase() .match(/&lt;[a-z][a-z0-9]*&gt;/g) || []) .join(''); // making sure the allowed arg is a string containing only tags in lowercase (&lt;a&gt;&lt;b&gt;&lt;c&gt;) var tags = /&lt;\/?([a-z][a-z0-9]*)\b[^&gt;]*&gt;/gi, commentsAndPhpTags = /&lt;!--[\s\S]*?--&gt;|&lt;\?(?:php)?[\s\S]*?\?&gt;/gi; return input.replace(commentsAndPhpTags, '') .replace(tags, function($0, $1) { return allowed.indexOf('&lt;' + $1.toLowerCase() + '&gt;') &gt; -1 ? $0 : ''; }); } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 14 '15 at 12:56">answered Jul 14 '15 at 12:56</time> <a href="../../users/406659/awebdeveloper" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/406659.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="aWebDeveloper" /> </a> <div class="s-user-card--info"> <a href="../../users/406659/awebdeveloper" class="s-user-card--link">aWebDeveloper</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">36,687</li> <li class="s-award-bling s-award-bling__gold" title="39 gold badges">39</li> <li class="s-award-bling s-award-bling__silver" title="170 silver badges">170</li> <li class="s-award-bling s-award-bling__bronze" title="242 bronze badges">242</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-31407272" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="31407272" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-58726456" class="comment js-comment " data-comment-id="58726456" data-comment-owner-id="407245" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment58726456_31407272"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You should quote the source (`phpjs`). If you use the `allowed` param you are vulnerable to XSS: `stripTags('<p onclick="alert(1)">mytext</p>', '<p>')` returns `</p><p onclick="alert(1)">mytext</p>`</span> –&nbsp;<a href="../../users/407245/chris-cinelli" title="4,679 reputation" class="comment-user ">Chris Cinelli</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment58726456_31407272"><span title="2016-02-20T01:25:29.987 License: CC BY-SA 3.0" class="relativetime-clean">Feb 20 '16 at 01:25</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="37472930"></a> <div id="answer-37472930" class="answer " data-answerid="37472930" data-ownerid="390330" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="37472930"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>The accepted answer works fine mostly, however in IE if the <code>html</code> string is <code>null</code> you get the <code>"null"</code> (instead of ''). Fixed: </p> <pre><code>function strip(html) { if (html == null) return ""; var tmp = document.createElement("DIV"); tmp.innerHTML = html; return tmp.textContent || tmp.innerText || ""; } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered May 27 '16 at 00:12">answered May 27 '16 at 00:12</time> <a href="../../users/390330/basarat" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/390330.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="basarat" /> </a> <div class="s-user-card--info"> <a href="../../users/390330/basarat" class="s-user-card--link">basarat</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">261,912</li> <li class="s-award-bling s-award-bling__gold" title="58 gold badges">58</li> <li class="s-award-bling s-award-bling__silver" title="460 silver badges">460</li> <li class="s-award-bling s-award-bling__bronze" title="511 bronze badges">511</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="4643986"></a> <div id="answer-4643986" class="answer " data-answerid="4643986" data-ownerid="513337" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="4643986"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I think the easiest way is to just use Regular Expressions as someone mentioned above. Although there's no reason to use a bunch of them. Try:</p> <pre><code>stringWithHTML = stringWithHTML.replace(/&lt;\/?[a-z][a-z0-9]*[^&lt;&gt;]*&gt;/ig, ""); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 10 '11 at 05:40">answered Jan 10 '11 at 05:40</time> <a href="../../users/513337/byron-carasco" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/513337.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Byron Carasco" /> </a> <div class="s-user-card--info"> <a href="../../users/513337/byron-carasco" class="s-user-card--link">Byron Carasco</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">107</li> <li class="s-award-bling s-award-bling__silver" title="2 silver badges">2</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-4643986" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="4643986" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-21501108" class="comment js-comment " data-comment-id="21501108" data-comment-owner-id="1463900" data-comment-score="14"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">14</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment21501108_4643986"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Don't do this if you care about security. If the user input is this: '<scr>ipt&gt;alert(42);</scr>ipt&gt;' then the stripped version will be this: '<script>alert(42);</script>'. So this is an XSS vulnerability.</span> –&nbsp;<a href="../../users/1463900/molnarg" title="2,775 reputation" class="comment-user ">molnarg</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment21501108_4643986"><span title="2013-03-06T12:38:45.470 License: CC BY-SA 3.0" class="relativetime-clean">Mar 06 '13 at 12:38</span></a></span> </div> </div> </li> <li id="comment-57312696" class="comment js-comment " data-comment-id="57312696" data-comment-owner-id="212378" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57312696_4643986"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">You should change the `[^&lt;&gt;]` with `[^&gt;]` because a valid tag cannot include a `&lt;` character, then the XSS vulnerability disappears.</span> –&nbsp;<a href="../../users/212378/alexis-wilke" title="19,179 reputation" class="comment-user ">Alexis Wilke</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment57312696_4643986"><span title="2016-01-14T08:00:59.547 License: CC BY-SA 3.0" class="relativetime-clean">Jan 14 '16 at 08:00</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="55346139"></a> <div id="answer-55346139" class="answer " data-answerid="55346139" data-ownerid="1522117" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="55346139"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>A safer way to strip the html with jQuery is to first use <a class="external-link" href="http://api.jquery.com/jQuery.parseHTML/" rel="nofollow noreferrer">jQuery.parseHTML</a> to create a DOM, ignoring any scripts, before letting jQuery build an element and then retrieving only the text.</p> <pre class="lang-js prettyprint-override"><code>function stripHtml(unsafe) { return $($.parseHTML(unsafe)).text(); } </code></pre> <p>Can safely strip html from:</p> <pre class="lang-html prettyprint-override"><code>&lt;img src="unknown.gif" onerror="console.log('running injections');"&gt; </code></pre> <p>And other exploits.</p> <p>nJoy!</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Jun 20 '20 at 09:12">edited Jun 20 '20 at 09:12</time> <a href="../../users/-1/community" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/-1.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Community" /> </a> <div class="s-user-card--info"> <a href="../../users/-1/community" class="s-user-card--link">Community</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Mar 25 '19 at 20:44">answered Mar 25 '19 at 20:44</time> <a href="../../users/1522117/nickl" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1522117.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="nickl-" /> </a> <div class="s-user-card--info"> <a href="../../users/1522117/nickl" class="s-user-card--link">nickl-</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">8,417</li> <li class="s-award-bling s-award-bling__gold" title="4 gold badges">4</li> <li class="s-award-bling s-award-bling__silver" title="42 silver badges">42</li> <li class="s-award-bling s-award-bling__bronze" title="56 bronze badges">56</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="70767709"></a> <div id="answer-70767709" class="answer " data-answerid="70767709" data-ownerid="10262805" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="70767709"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>const strip=(text) =&gt;{ return (new DOMParser()?.parseFromString(text,"text/html")) ?.body?.textContent } const value=document.getElementById("idOfEl").value const cleanText=strip(value) </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Apr 16 '22 at 10:26">edited Apr 16 '22 at 10:26</time> <a href="../../users/12002600/bitfinicon" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/12002600.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Bitfinicon" /> </a> <div class="s-user-card--info"> <a href="../../users/12002600/bitfinicon" class="s-user-card--link">Bitfinicon</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,045</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="8 silver badge">8</li> <li class="s-award-bling s-award-bling__bronze" title="22 bronze badge">22</li> </ul> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 19 '22 at 08:53">answered Jan 19 '22 at 08:53</time> <a href="../../users/10262805/yilmaz" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/10262805.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Yilmaz" /> </a> <div class="s-user-card--info"> <a href="../../users/10262805/yilmaz" class="s-user-card--link">Yilmaz</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">35,338</li> <li class="s-award-bling s-award-bling__gold" title="10 gold badges">10</li> <li class="s-award-bling s-award-bling__silver" title="157 silver badges">157</li> <li class="s-award-bling s-award-bling__bronze" title="202 bronze badges">202</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="74503121"></a> <div id="answer-74503121" class="answer " data-answerid="74503121" data-ownerid="6877799" data-score="2" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="74503121"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="2">2</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>A very good library would be <a class="external-link" href="https://www.npmjs.com/package/sanitize-html" rel="nofollow noreferrer"><code>sanitize-html</code></a> which is a pure JavaScript function and it could help in any environment.</p> <p>My case was on React Native I needed to remove all HTML tags from the given texts. so I created this wrapper function:</p> <pre class="lang-js prettyprint-override"><code>import sanitizer from 'sanitize-html'; const textSanitizer = (textWithHTML: string): string =&gt; sanitizer(textWithHTML, { allowedTags: [], }); export default textSanitizer; </code></pre> <p>Now by using my <code>textSanitizer</code>, I can have got the pure text contents.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 19 '22 at 19:43">answered Nov 19 '22 at 19:43</time> <a href="../../users/6877799/amerllica" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6877799.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="AmerllicA" /> </a> <div class="s-user-card--info"> <a href="../../users/6877799/amerllica" class="s-user-card--link">AmerllicA</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">29,059</li> <li class="s-award-bling s-award-bling__gold" title="15 gold badges">15</li> <li class="s-award-bling s-award-bling__silver" title="130 silver badges">130</li> <li class="s-award-bling s-award-bling__bronze" title="154 bronze badges">154</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="12250296"></a> <div id="answer-12250296" class="answer " data-answerid="12250296" data-ownerid="704916" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="12250296"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>With jQuery you can simply retrieving it by using </p> <pre><code>$('#elementID').text() </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Sep 03 '12 at 15:03">answered Sep 03 '12 at 15:03</time> <a href="../../users/704916/ianaz" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/704916.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="ianaz" /> </a> <div class="s-user-card--info"> <a href="../../users/704916/ianaz" class="s-user-card--link">ianaz</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">2,490</li> <li class="s-award-bling s-award-bling__gold" title="3 gold badges">3</li> <li class="s-award-bling s-award-bling__silver" title="28 silver badges">28</li> <li class="s-award-bling s-award-bling__bronze" title="37 bronze badges">37</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="13311633"></a> <div id="answer-13311633" class="answer " data-answerid="13311633" data-ownerid="1521108" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="13311633"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I have created a working regular expression myself:</p> <pre><code>str=str.replace(/(&lt;\?[a-z]*(\s[^&gt;]*)?\?(&gt;|$)|&lt;!\[[a-z]*\[|\]\]&gt;|&lt;!DOCTYPE[^&gt;]*?(&gt;|$)|&lt;!--[\s\S]*?(--&gt;|$)|&lt;[a-z?!\/]([a-z0-9_:.])*(\s[^&gt;]*)?(&gt;|$))/gi, ''); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 09 '12 at 16:06">answered Nov 09 '12 at 16:06</time> <a href="../../users/1521108/marekj47" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1521108.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="MarekJ47" /> </a> <div class="s-user-card--info"> <a href="../../users/1521108/marekj47" class="s-user-card--link">MarekJ47</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">143</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="4 bronze badges">4</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="17485349"></a> <div id="answer-17485349" class="answer " data-answerid="17485349" data-ownerid="1865916" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="17485349"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>simple 2 line jquery to strip the html.</p> <pre><code> var content = "&lt;p&gt;checking the html source&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;/p&gt;&lt;p&gt;with&amp;nbsp;&lt;/p&gt;&lt;p&gt;all&lt;/p&gt;&lt;p&gt;the html&amp;nbsp;&lt;/p&gt;&lt;p&gt;content&lt;/p&gt;"; var text = $(content).text();//It gets you the plain text console.log(text);//check the data in your console cj("#text_area_id").val(text);//set your content to text area using text_area_id </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 05 '13 at 09:18">answered Jul 05 '13 at 09:18</time> <a href="../../users/1865916/developer" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1865916.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Developer" /> </a> <div class="s-user-card--info"> <a href="../../users/1865916/developer" class="s-user-card--link">Developer</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">3,857</li> <li class="s-award-bling s-award-bling__gold" title="4 gold badges">4</li> <li class="s-award-bling s-award-bling__silver" title="37 silver badges">37</li> <li class="s-award-bling s-award-bling__bronze" title="47 bronze badges">47</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="41056319"></a> <div id="answer-41056319" class="answer " data-answerid="41056319" data-ownerid="6164984" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="41056319"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Using Jquery: </p> <pre><code>function stripTags() { return $('&lt;p&gt;&lt;/p&gt;').html(textToEscape).text() } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 09 '16 at 08:41">answered Dec 09 '16 at 08:41</time> <a href="../../users/6164984/math2001" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6164984.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="math2001" /> </a> <div class="s-user-card--info"> <a href="../../users/6164984/math2001" class="s-user-card--link">math2001</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">4,167</li> <li class="s-award-bling s-award-bling__silver" title="24 silver badges">24</li> <li class="s-award-bling s-award-bling__bronze" title="35 bronze badges">35</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="44547695"></a> <div id="answer-44547695" class="answer " data-answerid="44547695" data-ownerid="3434141" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="44547695"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p><code>input</code> element <a class="external-link" href="https://www.w3.org/wiki/HTML/Elements/input/text" rel="nofollow noreferrer">support only one line text</a>:</p> <blockquote> <p>The text state represents a one line plain text edit control for the element's value.</p> </blockquote> <pre><code>function stripHtml(str) { var tmp = document.createElement('input'); tmp.value = str; return tmp.value; } </code></pre> <p><strong>Update:</strong> this works as expected</p> <pre><code>function stripHtml(str) { // Remove some tags str = str.replace(/&lt;[^&gt;]+&gt;/gim, ''); // Remove BB code str = str.replace(/\[(\w+)[^\]]*](.*?)\[\/\1]/g, '$2 '); // Remove html and line breaks const div = document.createElement('div'); div.innerHTML = str; const input = document.createElement('input'); input.value = div.textContent || div.innerText || ''; return input.value; } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2017-10-27T02:13:08.733" class="relativetime">Oct 27 '17 at 02:13</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Mike Datsko</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 14 '17 at 14:32">answered Jun 14 '17 at 14:32</time> <a href="../../users/3434141/mike-datsko" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/3434141.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mike Datsko" /> </a> <div class="s-user-card--info"> <a href="../../users/3434141/mike-datsko" class="s-user-card--link">Mike Datsko</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">170</li> <li class="s-award-bling s-award-bling__bronze" title="11 bronze badges">11</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-44547695" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="44547695" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-80006777" class="comment js-comment " data-comment-id="80006777" data-comment-owner-id="773595" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment80006777_44547695"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Doesn't work, please always mention the browser you are using when posting an answer. This is inaccurate and won't work in Chrome 61. Tags are just rendered as a string.</span> –&nbsp;<a href="../../users/773595/vdegenne" title="12,272 reputation" class="comment-user ">vdegenne</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment80006777_44547695"><span title="2017-10-02T13:26:27.407 License: CC BY-SA 3.0" class="relativetime-clean">Oct 02 '17 at 13:26</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="68253741"></a> <div id="answer-68253741" class="answer " data-answerid="68253741" data-ownerid="6144731" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="68253741"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>If you don't want to create a DOM for this (perhaps you're not in a browser context) you could use the <a class="external-link" href="https://www.npmjs.com/package/striptags" rel="nofollow noreferrer">striptags</a> npm package.</p> <pre class="lang-js prettyprint-override"><code>import striptags from 'striptags'; //ES6 &lt;-- pick one const striptags = require('striptags'); //ES5 &lt;-- pick one striptags('&lt;p&gt;An HTML string&lt;/p&gt;'); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 05 '21 at 09:31">answered Jul 05 '21 at 09:31</time> <a href="../../users/6144731/jnaklaas" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6144731.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="jnaklaas" /> </a> <div class="s-user-card--info"> <a href="../../users/6144731/jnaklaas" class="s-user-card--link">jnaklaas</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,619</li> <li class="s-award-bling s-award-bling__silver" title="13 silver badges">13</li> <li class="s-award-bling s-award-bling__bronze" title="16 bronze badges">16</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="70640761"></a> <div id="answer-70640761" class="answer " data-answerid="70640761" data-ownerid="11891319" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="70640761"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>const getTextFromHtml = (t) =&gt; t ?.split('&gt;') ?.map((i) =&gt; i.split('&lt;')[0]) .filter((i) =&gt; !i.includes('=') &amp;&amp; i.trim()) .join(''); const test = '&lt;p&gt;This &lt;strong&gt;one&lt;/strong&gt; &lt;em&gt;time&lt;/em&gt;,&lt;/p&gt;&lt;br /&gt;&lt;blockquote&gt;I went to&lt;/blockquote&gt;&lt;ul&gt;&lt;li&gt;band &lt;a href="https://workingclasshistory.com" rel="noopener noreferrer" target="_blank"&gt;camp&lt;/a&gt;…&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I edited this as a reviewer just to double check&lt;/p&gt;' getTextFromHtml(test) // 'This onetime,I went toband camp…I edited this as a reviewer just to double check' </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 09 '22 at 11:19">answered Jan 09 '22 at 11:19</time> <a href="../../users/11891319/fadi-omar" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/11891319.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="fadi omar" /> </a> <div class="s-user-card--info"> <a href="../../users/11891319/fadi-omar" class="s-user-card--link">fadi omar</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">740</li> <li class="s-award-bling s-award-bling__silver" title="5 silver badges">5</li> <li class="s-award-bling s-award-bling__bronze" title="15 bronze badges">15</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-70640761" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="70640761" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-125219842" class="comment js-comment " data-comment-id="125219842" data-comment-owner-id="12002600" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment125219842_70640761"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Thanks it works but not strips,  </span> –&nbsp;<a href="../../users/12002600/bitfinicon" title="1,045 reputation" class="comment-user ">Bitfinicon</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment125219842_70640761"><span title="2022-01-24T10:14:59.983 License: CC BY-SA 4.0" class="relativetime-clean">Jan 24 '22 at 10:14</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="71758885"></a> <div id="answer-71758885" class="answer " data-answerid="71758885" data-ownerid="1754995" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="71758885"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>As others suggested, I recommend using <code>DOMParser</code> when possible.</p> <p>However, if you happen to be working inside a <strong>Node</strong>/<strong>JS</strong> Lambda or otherwise <code>DOMParser</code> is not available, I came up with the regex below to match most of the scenarios mentioned in previous answers/comments. It doesn't match <code>$gt;</code> and <code>$lt;</code> as some others may have a concern about, but should capture pretty much any other scenario.</p> <pre><code>const dangerousText = '?'; const htmlTagRegex = /&lt;\/?([a-zA-Z]\s?)*?([a-zA-Z]+?=\s?".*")*?([\s/]*?)&gt;/gi; const sanitizedText = dangerousText.replace(htmlTagRegex, ''); </code></pre> <p>This might be easy to simplify, but it should work for most situations. Hope it helps someone.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 05 '22 at 22:01">answered Apr 05 '22 at 22:01</time> <a href="../../users/1754995/kody" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1754995.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Kody" /> </a> <div class="s-user-card--info"> <a href="../../users/1754995/kody" class="s-user-card--link">Kody</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">905</li> <li class="s-award-bling s-award-bling__silver" title="9 silver badges">9</li> <li class="s-award-bling s-award-bling__bronze" title="19 bronze badges">19</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-71758885" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="71758885" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-126813958" class="comment js-comment " data-comment-id="126813958" data-comment-owner-id="1754995" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment126813958_71758885"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`'<div data="a &gt; b &lt; c">Test</div>'.replace(/&lt;\/?([a-zA-Z]\s?)*?([a-zA-Z]+?=\s?".*")*?([\s/]*?)&gt;/gi, '')`</span> –&nbsp;<a href="../../users/1754995/kody" title="905 reputation" class="comment-user ">Kody</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment126813958_71758885"><span title="2022-04-05T22:09:45.880 License: CC BY-SA 4.0" class="relativetime-clean">Apr 05 '22 at 22:09</span></a></span> </div> </div> </li> <li id="comment-126813960" class="comment js-comment " data-comment-id="126813960" data-comment-owner-id="1754995" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment126813960_71758885"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">`'<img alt="a&gt;b"/>Test'.replace(/&lt;\/?([a-zA-Z]\s?)*?([a-zA-Z]+?=\s?".*")*?([\s/]*?)&gt;/gi, '')`</span> –&nbsp;<a href="../../users/1754995/kody" title="905 reputation" class="comment-user ">Kody</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment126813960_71758885"><span title="2022-04-05T22:09:49.930 License: CC BY-SA 4.0" class="relativetime-clean">Apr 05 '22 at 22:09</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="40484875"></a> <div id="answer-40484875" class="answer " data-answerid="40484875" data-ownerid="4264884" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40484875"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>For escape characters also this will work using pattern matching:</p> <pre><code>myString.replace(/((&amp;lt)|(&lt;)(?:.|\n)*?(&amp;gt)|(&gt;))/gm, ''); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2016-11-16T06:00:59.803" class="relativetime">Nov 16 '16 at 06:00</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Abhishek Dhanraj Shahdeo</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 08 '16 at 10:44">answered Nov 08 '16 at 10:44</time> <a href="../../users/4264884/abhishek-dhanraj-shahdeo" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4264884.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Abhishek Dhanraj Shahdeo" /> </a> <div class="s-user-card--info"> <a href="../../users/4264884/abhishek-dhanraj-shahdeo" class="s-user-card--link">Abhishek Dhanraj Shahdeo</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,356</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="14 silver badges">14</li> <li class="s-award-bling s-award-bling__bronze" title="35 bronze badges">35</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="54606362"></a> <div id="answer-54606362" class="answer " data-answerid="54606362" data-ownerid="4753198" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="54606362"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p><a class="external-link" href="https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML</a></p> <pre><code>var div = document.getElementsByTagName('div'); for (var i=0; i&lt;div.length; i++) { div[i].insertAdjacentHTML('afterend', div[i].innerHTML); document.body.removeChild(div[i]); } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Feb 09 '19 at 12:48">answered Feb 09 '19 at 12:48</time> <a href="../../users/4753198/sonichy" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4753198.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="sonichy" /> </a> <div class="s-user-card--info"> <a href="../../users/4753198/sonichy" class="s-user-card--link">sonichy</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,370</li> <li class="s-award-bling s-award-bling__gold" title="2 gold badges">2</li> <li class="s-award-bling s-award-bling__silver" title="14 silver badges">14</li> <li class="s-award-bling s-award-bling__bronze" title="17 bronze badges">17</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="59339309"></a> <div id="answer-59339309" class="answer " data-answerid="59339309" data-ownerid="10355515" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="59339309"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>method 1:</p> <pre><code>function cleanHTML(str){ str.replace(/&lt;(?&lt;=&lt;)(.*?)(?=&gt;)&gt;/g, '&amp;lt;$1&amp;gt;'); } function uncleanHTML(str){ str.replace(/&amp;lt;(?&lt;=&amp;lt;)(.*?)(?=&amp;gt;)&amp;gt;/g, '&lt;$1&gt;'); } </code></pre> <p>method 2:</p> <pre><code>function cleanHTML(str){ str.replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;'); } function uncleanHTML(str){ str.replace(/&amp;lt;/g, '&lt;').replace(/&amp;gt;/g, '&gt;'); } </code></pre> <p>also, don't forget if the user happens to post a math comment <code>(ex: 1 &lt; 2)</code>, you don't want to strip the whole comment. The browser (only tested chrome) doesn't run unicode as html tags. if you replace all <code>&lt;</code> with <code>&amp;lt;</code> everyware in the string, the unicode will display <code>&lt;</code> as text without running any html. I recommend method 2. jquery also works well <code>$('#element').text();</code></p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2019-12-15T02:31:37.847" class="relativetime">Dec 15 '19 at 02:31</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">SwiftNinjaPro</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Dec 14 '19 at 21:28">answered Dec 14 '19 at 21:28</time> <a href="../../users/10355515/swiftninjapro" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/10355515.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="SwiftNinjaPro" /> </a> <div class="s-user-card--info"> <a href="../../users/10355515/swiftninjapro" class="s-user-card--link">SwiftNinjaPro</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">787</li> <li class="s-award-bling s-award-bling__silver" title="8 silver badges">8</li> <li class="s-award-bling s-award-bling__bronze" title="17 bronze badges">17</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="67889717"></a> <div id="answer-67889717" class="answer " data-answerid="67889717" data-ownerid="6306323" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="67889717"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>var STR='&lt;Your HTML STRING&gt;'' var HTMLParsedText=""; var resultSet = STR.split('&gt;') var resultSetLength =resultSet.length var counter=0 while(resultSetLength&gt;0) { if(resultSet[counter].indexOf('&lt;')&gt;0) { var value = resultSet[counter]; value=value.substring(0, resultSet[counter].indexOf('&lt;')) if (resultSet[counter].indexOf('&amp;')&gt;=0 &amp;&amp; resultSet[counter].indexOf(';')&gt;=0) { value=value.replace(value.substring(resultSet[counter].indexOf('&amp;'), resultSet[counter].indexOf(';')+1),'') } } if (value) { value = value.trim(); if(HTMLParsedText === "") { HTMLParsedText = value; } else { if (value) { HTMLParsedText = HTMLParsedText + "\n" + value; } } value=''; } counter= counter+1; resultSetLength=resultSetLength-1; } console.log(HTMLParsedText); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 08 '21 at 15:07">answered Jun 08 '21 at 15:07</time> <a href="../../users/6306323/saurabh-dixit" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6306323.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Saurabh Dixit" /> </a> <div class="s-user-card--info"> <a href="../../users/6306323/saurabh-dixit" class="s-user-card--link">Saurabh Dixit</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">11</li> <li class="s-award-bling s-award-bling__bronze" title="1 bronze badges">1</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="68334400"></a> <div id="answer-68334400" class="answer " data-answerid="68334400" data-ownerid="188740" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="68334400"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>This package works really well for stripping HTML: <a class="external-link" href="https://www.npmjs.com/package/string-strip-html" rel="nofollow noreferrer">https://www.npmjs.com/package/string-strip-html</a></p> <p>It works in both the browser and on the server (e.g. Node.js).</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 11 '21 at 08:13">answered Jul 11 '21 at 08:13</time> <a href="../../users/188740/johnny-oshika" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/188740.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Johnny Oshika" /> </a> <div class="s-user-card--info"> <a href="../../users/188740/johnny-oshika" class="s-user-card--link">Johnny Oshika</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">54,741</li> <li class="s-award-bling s-award-bling__gold" title="40 gold badges">40</li> <li class="s-award-bling s-award-bling__silver" title="181 silver badges">181</li> <li class="s-award-bling s-award-bling__bronze" title="275 bronze badges">275</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="73906513"></a> <div id="answer-73906513" class="answer " data-answerid="73906513" data-ownerid="10842627" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="73906513"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>You can strip out all the html tags with the following regex: <strong>/&lt;(.|\n)*?&gt;/g</strong></p> <p>Example:</p> <pre><code>let str = "&lt;font class=\"ClsName\"&gt;int[0]&lt;/font&gt;&lt;font class=\"StrLit\"&gt;()&lt;/font&gt;"; console.log(str.replace(/&lt;(.|\n)*?&gt;/g, '')); </code></pre> <p>Output:</p> <pre><code>int[0]() </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2022-09-30T09:29:15.543" class="relativetime">Sep 30 '22 at 09:29</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Mahesh</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Sep 30 '22 at 09:17">answered Sep 30 '22 at 09:17</time> <a href="../../users/10842627/mahesh" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/10842627.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mahesh" /> </a> <div class="s-user-card--info"> <a href="../../users/10842627/mahesh" class="s-user-card--link">Mahesh</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">103</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="10 bronze badges">10</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="75901892"></a> <div id="answer-75901892" class="answer " data-answerid="75901892" data-ownerid="1117787" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="75901892"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>Additionally if you want to strip the html from a string and preserve the break lines, you can use this:</p> <pre><code>function stripHTML(string)( let doc = new DOMParser().parseFromString(string, 'text/html'); let textLines = []; doc.body.childNodes.forEach((childNode) =&gt; { textLines.push(childNode.textContent || ''); }) let result = textLines.join('&lt;br&gt;'); return result; ) </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Mar 31 '23 at 18:57">answered Mar 31 '23 at 18:57</time> <a href="../../users/1117787/mariano-arganaraz" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1117787.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mariano Argañaraz" /> </a> <div class="s-user-card--info"> <a href="../../users/1117787/mariano-arganaraz" class="s-user-card--link">Mariano Argañaraz</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,159</li> <li class="s-award-bling s-award-bling__silver" title="11 silver badges">11</li> <li class="s-award-bling s-award-bling__bronze" title="22 bronze badges">22</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="76424919"></a> <div id="answer-76424919" class="answer " data-answerid="76424919" data-ownerid="15266227" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="76424919"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>To add to the DOMParser solution. Our team found that it was still possible to inject malicious script using the basic solution.</p> <p><code>\"&gt;&lt;script&gt;document.write('&lt;img src=//X55.is onload=import(src)&gt;');&lt;/script&gt;'</code></p> <p><code>\"&gt;&lt;script&gt;document.write('\"&gt;&lt;script&gt;document.write('\"&gt;&lt;img src=//X55.is onload=import(src)&gt;');&lt;/script&gt;');&lt;/script&gt;</code></p> <p>We found that it was best to parse it recursively if any tags still exist after the initial parse.</p> <pre class="lang-js prettyprint-override"><code>function stripHTML(str) { const parsedHTML = new DOMParser().parseFromString(str, "text/html"); const text = parsedHTML.body.textContent; if (/(&lt;([^&gt;]+)&gt;)/gi.test(text)) { return stripHTML(text); } return text || ""; } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jun 07 '23 at 15:31">answered Jun 07 '23 at 15:31</time> <a href="../../users/15266227/samuel-eiche" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/15266227.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Samuel Eiche" /> </a> <div class="s-user-card--info"> <a href="../../users/15266227/samuel-eiche" class="s-user-card--link">Samuel Eiche</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">139</li> <li class="s-award-bling s-award-bling__bronze" title="7 bronze badges">7</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="9732883"></a> <div id="answer-9732883" class="answer " data-answerid="9732883" data-ownerid="1273360" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="9732883"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code> (function($){ $.html2text = function(html) { if($('#scratch_pad').length === 0) { $('&lt;div id="lh_scratch"&gt;&lt;/div&gt;').appendTo('body'); } return $('#scratch_pad').html(html).text(); }; })(jQuery); </code></pre> <p>Define this as a jquery plugin and use it like as follows:</p> <pre><code>$.html2text(htmlContent); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Mar 16 '12 at 06:25">answered Mar 16 '12 at 06:25</time> <a href="../../users/1273360/shiv-shankar" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1273360.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Shiv Shankar" /> </a> <div class="s-user-card--info"> <a href="../../users/1273360/shiv-shankar" class="s-user-card--link">Shiv Shankar</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">9</li> <li class="s-award-bling s-award-bling__bronze" title="1 bronze badges">1</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-9732883" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="9732883" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-89653315" class="comment js-comment " data-comment-id="89653315" data-comment-owner-id="3989484" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment89653315_9732883"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Lets say this comes from user input. It can be used to add script or macros to your page</span> –&nbsp;<a href="../../users/3989484/oluwatumbi" title="1,321 reputation" class="comment-user ">Oluwatumbi</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment89653315_9732883"><span title="2018-07-14T13:26:02.580 License: CC BY-SA 4.0" class="relativetime-clean">Jul 14 '18 at 13:26</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="51181183"></a> <div id="answer-51181183" class="answer " data-answerid="51181183" data-ownerid="9229032" data-score="-1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="51181183"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="-1">-1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><pre><code>function strip_html_tags(str) { if ((str===null) || (str==='')) return false; else str = str.toString(); return str.replace(/&lt;[^&gt;]*&gt;/g, ''); } </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jul 04 '18 at 21:59">answered Jul 04 '18 at 21:59</time> <a href="../../users/9229032/jonathan-utsu-undelikwo" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/9229032.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Jonathan Utsu Undelikwo" /> </a> <div class="s-user-card--info"> <a href="../../users/9229032/jonathan-utsu-undelikwo" class="s-user-card--link">Jonathan Utsu Undelikwo</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">642</li> <li class="s-award-bling s-award-bling__gold" title="1 gold badge">1</li> <li class="s-award-bling s-award-bling__silver" title="5 silver badge">5</li> <li class="s-award-bling s-award-bling__bronze" title="9 bronze badge">9</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-51181183" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="51181183" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-89349592" class="comment js-comment " data-comment-id="89349592" data-comment-owner-id="445131" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment89349592_51181183"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Rolling your own like this is at best a half-solution because this will just nuke the `<br/>` newlines and remove other meaningful information. It removes angle brackets and their interiors from a body of text which leaves you in an inconsistent state. Plus the non angle-bracketed HTML code wouldn't be affected. This is a start I guess, but a function to do this would be at minimum several hundred lines.</span> –&nbsp;<a href="../../users/445131/eric-leschinski" title="146,994 reputation" class="comment-user ">Eric Leschinski</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/822452/strip-html-tags-from-text-using-plain-javascript#comment89349592_51181183"><span title="2018-07-05T05:30:44.387 License: CC BY-SA 4.0" class="relativetime-clean">Jul 05 '18 at 05:30</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> </div> </div> <div id="sidebar" class="show-votes" role="complementary" aria-label="sidebar"> <div class="module sidebar-linked"> <h4 id="h-linked">Linked</h4> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/27808792/replace-starting-and-ending-multiple-html-tags-using-javascript" class="question-hyperlink">Replace starting and ending multiple html tags using javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/28043367/how-to-remove-tag-between-string-using-javascript" class="question-hyperlink">How to remove tag between string using javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/28331383/remove-invalid-characters-from-javascript" class="question-hyperlink">Remove invalid characters from Javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/28440721/javascript-strip-html-tags-and-their-contents-from-a-string" class="question-hyperlink">Javascript - Strip HTML tags and their contents from a string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/28544037/jquery-on-event-remove-all-tags-except-span" class="question-hyperlink">jquery on event remove all tags except span</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/28693082/how-can-i-output-this-content-to-a-textarea" class="question-hyperlink">How can i output this content to a textarea?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">3</div></a> <a href="../../questions/29936055/format-hyperlinks-when-exporting-html-table-to-excel" class="question-hyperlink">Format hyperlinks when exporting HTML table to Excel</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/30112631/remove-html-injection" class="question-hyperlink">Remove html injection?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/30445628/htmltoplaintext-angularjs-1-3-15-custom-filter" class="question-hyperlink">&quot;htmlToPlaintext&quot; AngularJS (1.3.15) Custom Filter</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-3</div></a> <a href="../../questions/30796454/how-to-get-html-element-s-text" class="question-hyperlink">How to get Html Element's Text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">11</div></a> <a href="../../questions/31515999/how-to-remove-all-html-tags-from-a-string" class="question-hyperlink">How to remove all html tags from a string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/31830097/strip-all-unwanted-tags-from-html-string-but-preserve-whitespace-in-js" class="question-hyperlink">Strip all unwanted tags from html string but preserve whitespace in JS</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/32475157/find-text-in-html-with-regex" class="question-hyperlink">Find Text in HTML with Regex</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-2</div></a> <a href="../../questions/33588514/how-to-remove-string-between-two-characters-every-time-they-occur" class="question-hyperlink">How to remove string between two characters every time they occur</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-3</div></a> <a href="../../questions/33666429/remove-all-html-with-regex-in-javascript-not-typical" class="question-hyperlink">Remove all HTML with Regex in Javascript (not typical)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">7</div></a> <a href="../../questions/22398635/how-to-strip-html-tags-from-div-content-using-javascript-jquery" class="question-hyperlink">How to strip HTML tags from div content using Javascript/jQuery?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/34062245/checking-for-html-markup-in-an-rss-feed-and-removing" class="question-hyperlink">Checking for HTML markup in an RSS feed and removing</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/34455783/highcharts-generate-chart-from-tables-having-linked-data" class="question-hyperlink">Highcharts: Generate chart from tables having linked data</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/34498594/how-to-remove-html-tags-from-knockout-text-binding" class="question-hyperlink">How to remove HTML tags from Knockout Text binding?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">23</div></a> <a href="../../questions/4321896/is-there-a-faster-way-to-decode-html-characters-to-a-string-than-html-fromhtml" class="question-hyperlink">Is there a faster way to decode html characters to a string than Html.fromHtml()?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/34920926/convert-html-to-string-using-angularjs" class="question-hyperlink">Convert html to string using angularjs</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/35563845/want-to-make-a-read-more-option" class="question-hyperlink">Want to make a Read more option</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/36538935/javascript-select-only-inner-html-of-outer-element-excluding-inner-element" class="question-hyperlink">JavaScript select only inner html of outer element excluding inner element</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/36773918/jquery-word-count-and-stripping-tags-from-html" class="question-hyperlink">JQuery word count and stripping tags from HTML</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/36812694/javascript-remove-one-html-tag-with-a-specific-structure" class="question-hyperlink">Javascript remove one html tag with a specific structure</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/37002700/adjust-index-for-html-string-to-compensate-for-html-tags" class="question-hyperlink">Adjust index for HTML string to compensate for HTML tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/37218429/remove-all-html-tags-except-img-or-img-tags-with-javascript" class="question-hyperlink">Remove all html tags except &lt;img&gt; or &lt;img/&gt; tags with javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/39039731/sanitize-ajax-response-that-updates-options-in-select-box" class="question-hyperlink">Sanitize AJAX Response that updates Options in Select Box</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/39530203/html-tags-i-e-p-inside-my-angular-brackets" class="question-hyperlink">HTML tags (i.e &lt;p&gt;) inside my angular brackets {{}}</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-1</div></a> <a href="../../questions/39686626/want-to-remove-image-path-from-text-file" class="question-hyperlink">Want to remove image path from text file?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/40104592/remove-html-tags-in-script" class="question-hyperlink">Remove HTML tags in script</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/40653873/how-to-remove-html-tag-from-string-but-keep-html-entities-intact" class="question-hyperlink">How to remove html tag from string but keep html entities intact</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/40798194/sentence-case-a-passage-of-text-while-ignoring-html-elements-within" class="question-hyperlink">Sentence case a passage of text while ignoring html elements within</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/42095572/removing-an-anchor-tag-a-href-a-from-an-array-of-markup-elements" class="question-hyperlink">Removing an anchor tag `&lt;a href=&quot;&quot;&gt;&lt;/a&gt;` from an array of markup elements</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/42557038/how-to-remove-html-code-with-javascript" class="question-hyperlink">How to remove HTML Code with Javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/44521684/deal-with-spaces-in-this-regex" class="question-hyperlink">Deal with spaces in this regex</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/44864900/displaying-external-site-in-plain-text" class="question-hyperlink">Displaying external site in plain text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">3</div></a> <a href="../../questions/45780368/sort-array-but-ignore-some-specific-characters-in-the-beginning" class="question-hyperlink">Sort array, but ignore some specific characters in the beginning</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">4</div></a> <a href="../../questions/46375936/how-to-search-the-text-between-the-html-tags" class="question-hyperlink">How to search the text between the HTML tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">3</div></a> <a href="../../questions/46598890/vuejs-convert-html-to-plain-text" class="question-hyperlink">Vuejs Convert HTML to Plain text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/47156164/how-to-strip-the-html-tag-from-string" class="question-hyperlink">How to strip the &lt;html&gt; Tag from string?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/47677238/removing-tags-for-a-string-with-recursion" class="question-hyperlink">removing tags for a string with recursion</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/48071100/javascript-loop-strings-in-between-selected-characters" class="question-hyperlink">Javascript loop strings in between selected characters</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/48280624/strip-ajax-response-from-ascii-signs" class="question-hyperlink">Strip ajax response from ASCII? signs</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/48661174/how-to-remove-html-text-attribute-tags-using-javascript" class="question-hyperlink">How to remove html text attribute tags using javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-2</div></a> <a href="../../questions/48826533/how-to-filter-out-html-tags-from-array-and-replace-with-nothing" class="question-hyperlink">How to filter out HTML tags from array and replace with nothing?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/49239926/truncating-or-summarizing-text-with-html-part-in-html" class="question-hyperlink">Truncating or Summarizing Text with html part in HTML</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">3</div></a> <a href="../../questions/49794417/javascript-remove-html-tags-modify-tags-text-and-insert-tags-back-in" class="question-hyperlink">JavaScript: Remove HTML Tags, Modify Tags/Text, and Insert Tags Back In</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/45317703/how-to-truncate-html-content-in-angular-2" class="question-hyperlink">How to truncate HTML content in Angular 2?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/50819141/jquery-autocomplete-change-search-substring" class="question-hyperlink">JQuery autocomplete, change search substring</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-1</div></a> <a href="../../questions/51229344/regex-don-t-match-if-inner-html-tags-exist" class="question-hyperlink">RegEx - Don't match if inner HTML tags exist</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/51354392/my-function-is-taking-too-long-to-load" class="question-hyperlink">My function is taking too long to load</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">4</div></a> <a href="../../questions/51620643/headless-wordpress-api-how-to-remove-html-in-json" class="question-hyperlink">Headless Wordpress API: how to remove html in JSON</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/52266692/how-to-get-rid-of-everything-inside-brackets-in-javascript" class="question-hyperlink">How to get rid of everything inside &lt;&gt; brackets in Javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/52649891/ignoring-html-tags-in-regexp" class="question-hyperlink">Ignoring html tags in regexp</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-5</div></a> <a href="../../questions/52759321/how-to-hide-html-elements" class="question-hyperlink">how to hide html elements?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/52816917/find-length-of-string-without-counting-included-html-tags" class="question-hyperlink">Find length of string without counting included html tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/53698843/replacing-quot-with-using-common-methods-does-not-work-in-a-javascript-azure-f" class="question-hyperlink">Replacing &amp;quot; with &quot; using common methods does not work in a JavaScript Azure Function</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/54082134/how-to-delete-html-tags-form-responsejson-in-react-native" class="question-hyperlink">How to Delete html tags form responseJson in react native?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-2</div></a> <a href="../../questions/54153455/extract-text-values-from-a-string-like-html" class="question-hyperlink">extract text values from a string like html</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/54272287/how-to-get-plain-text-from-wiki-api-subcategory" class="question-hyperlink">How to get plain text from Wiki API subcategory</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/54828984/js-how-to-get-a-string-of-outerhtml-textcontent" class="question-hyperlink">JS - how to get a string of outerHTML textContent?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-3</div></a> <a href="../../questions/55435890/array-order-in-sorted-table" class="question-hyperlink">Array order in sorted table</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/55689554/javascript-replace-text-inside-div-foo-span-text-span-bar-div-by-div-foo-text-" class="question-hyperlink">Javascript: replace text inside &lt;div&gt;foo &lt;span&gt;text &lt;/span&gt;bar&lt;/div&gt; by &lt;div&gt;foo text bar&lt;/div&gt;</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/55730919/get-text-out-of-node-html-in-variable-text-given" class="question-hyperlink">Get text out of node HTML in Variable Text Given</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/56999728/how-to-remove-trim-xml-tags-using-javascript-code" class="question-hyperlink">How to remove/trim .xml tags using javascript code</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">39</div></a> <a href="../../questions/40263803/native-javascript-or-es6-way-to-encode-and-decode-html-entities" class="question-hyperlink">Native JavaScript or ES6 way to encode and decode HTML entities?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/58982810/how-to-read-embedded-html-string-in-js" class="question-hyperlink">How to read embedded html string in js?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">161</div></a> <a href="../../questions/6743912/how-to-get-the-pure-text-without-html-element-using-javascript" class="question-hyperlink">How to get the pure text without HTML element using JavaScript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">402</div></a> <a href="../../questions/10585029/parse-an-html-string-with-js" class="question-hyperlink">Parse an HTML string with JS</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">2</div></a> <a href="../../questions/60779184/how-can-i-remove-all-html-elements-from-a-string-excluding-a-special-class" class="question-hyperlink">How can I remove all HTML elements from a string excluding a special class?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/60841762/how-to-strip-html-tags-from-a-dropdown-option-list" class="question-hyperlink">How to strip HTML tags from a dropdown option list?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/63601163/remove-text-and-tag-in-string" class="question-hyperlink">Remove text and tag in string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/27686693/getting-plain-text-from-imperavi-redactor" class="question-hyperlink">Getting Plain Text from Imperavi Redactor</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/65386359/nodejs-block-html-in-socket-io-messages" class="question-hyperlink">nodejs block html in socket.io messages</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">3</div></a> <a href="../../questions/65627253/how-do-you-truncate-html-text-in-react" class="question-hyperlink">How do you truncate html text in React?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/67523410/jquery-remove-i-and-i-tag-from-string" class="question-hyperlink">Jquery remove &lt;i&gt; and &lt;/i&gt; tag from string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/67583250/how-to-get-html-tag-from-api-response-in-react-js" class="question-hyperlink">How to get HTML tag from API response in React Js?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/67687547/how-to-exclude-style-from-copied-text-in-prime-ng-editor" class="question-hyperlink">How to exclude style from copied text in Prime NG editor</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/68267671/vuejs-html-re-render-or-strip-the-html-tags" class="question-hyperlink">Vuejs, HTML: re-render or strip the html tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">70</div></a> <a href="../../questions/53728/will-html-encoding-prevent-all-kinds-of-xss-attacks" class="question-hyperlink">Will HTML Encoding prevent all kinds of XSS attacks?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/68958762/convert-html-data-into-text-using-ejs" class="question-hyperlink">convert Html data into text using ejs</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/69949942/js-how-can-i-remove-text-under-conditions-of-starting-at-and-ending-at" class="question-hyperlink">JS, How can I remove text under conditions of starting at &lt; and ending at &gt;?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/69988075/data-table-escape-regex-dropdown-list-filter-not-working" class="question-hyperlink">Data table escape regex - dropdown list filter not working</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/70981639/changing-color-of-a-certain-word-in-a-paragraph-using-js" class="question-hyperlink">Changing color of a certain word in a paragraph using js</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/71224230/how-to-use-external-scripts-e-g-jquery-for-youtrack-workflow-scripts" class="question-hyperlink">How to use external scripts e.g. jquery for YouTrack Workflow scripts</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/71595485/i-need-to-get-a-script-using-cherrio-not-get-text" class="question-hyperlink">I need to get a script using cherrio (not get text)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/71950998/remove-from-varialble-of-html-codeblock-next-js" class="question-hyperlink">remove &quot;&quot; from varialble of html codeblock next js</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/72200353/complex-search-and-replace-strings-with-javascript" class="question-hyperlink">complex search and replace strings with JavaScript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/72514469/how-to-safely-truncate-text-containing-html-tags-with-javascript" class="question-hyperlink">how to safely truncate text containing html tags with javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/72813750/how-to-strip-html-from-text-using-javascript-but-keep-a-space-between-each-lin" class="question-hyperlink">How to strip HTML from text using JavaScript but keep a space between each line?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/73225259/calculating-word-count-after-stripping-html" class="question-hyperlink">Calculating word count after stripping HTML</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/74517628/remove-html-tags-and-its-contents-from-a-string-javascript" class="question-hyperlink">Remove HTML tags and its contents from a string - Javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">3</div></a> <a href="../../questions/76778342/convert-html-to-readable-text" class="question-hyperlink">Convert HTML to readable text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">14</div></a> <a href="../../questions/3813167/what-is-the-most-convenient-way-to-convert-html-to-plain-text-while-preserving" class="question-hyperlink">What is the most convenient way to convert HTML to plain text while preserving line breaks (with JavaScript)?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">5</div></a> <a href="../../questions/2966796/how-to-get-a-webpage-as-plain-text-without-any-html-using-javascript" class="question-hyperlink">How to get a webpage as plain text without any html using javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/7116894/want-to-remove-any-html-tags" class="question-hyperlink">Want to remove any HTML tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/7540308/remove-html-tags-from-string-using-jquery" class="question-hyperlink">Remove html tags from string using jquery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">6</div></a> <a href="../../questions/7540489/javascript-regex-match-text-not-part-of-a-html-tag" class="question-hyperlink">Javascript Regex: Match text NOT part of a HTML tag</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/7545008/how-to-obtain-entire-text-content-of-a-web-page-within-a-iframe-using-javascri" class="question-hyperlink">how to obtain entire text content of a web page within a iframe using javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">40</div></a> <a href="../../questions/7889765/remove-all-htmltags-in-a-string-with-the-jquery-text-function" class="question-hyperlink">Remove all HTMLtags in a string (with the jquery text() function)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/8709882/get-rid-or-hide-html-tags-from-textarea-in-javascript" class="question-hyperlink">Get rid or hide html tags from textarea in Javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/9200355/javascript-converting-plain-text-to-links-smilies" class="question-hyperlink">JavaScript converting plain text to links &amp;&amp; smilies</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/9460585/transfer-possibly-unsafe-user-input-from-hidden-input-field-to-dom" class="question-hyperlink">Transfer possibly unsafe user input from hidden input field to DOM</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/9465860/textarea-set-html-value" class="question-hyperlink">Textarea set HTML Value</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/9617670/how-to-check-input-tag-have-only-tag-br-inside-as-value-using-jquery" class="question-hyperlink">How to check input tag have only tag(&lt;br/&gt;) inside as value using jQuery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">28</div></a> <a href="../../questions/9727111/innerhtml-without-the-html-just-text" class="question-hyperlink">innerHTML without the html, just text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/10178484/js-remove-certain-words-that-don-t-match" class="question-hyperlink">js remove certain words that don't match</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/10448596/hide-remove-piece-of-text" class="question-hyperlink">Hide/remove piece of text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/11763045/remove-html-from-input-jquery" class="question-hyperlink">remove HTML from input jquery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/12223090/microsoft-word-metadata-on-copy-and-paste-into-html-contenteditable" class="question-hyperlink">Microsoft Word Metadata on copy and paste into html (contenteditable)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/12406261/iframe-is-adding-tags-need-to-remove-them" class="question-hyperlink">iframe is adding tags, need to remove them</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-3</div></a> <a href="../../questions/12941663/removing-html-tags-from-a-string-and-keeping-colon" class="question-hyperlink">Removing HTML tags from a string and keeping &quot;:&quot; (colon)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/12957132/create-a-regular-expression-with-the-tags" class="question-hyperlink">Create a regular expression with the tags?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">79</div></a> <a href="../../questions/13140043/how-to-strip-html-tags-with-jquery" class="question-hyperlink">How to strip HTML tags with jQuery?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/13446592/retrieving-values-from-a-string-using-jquery" class="question-hyperlink">Retrieving values from a string using jQuery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">30</div></a> <a href="../../questions/13911681/remove-html-tags-from-a-javascript-string" class="question-hyperlink">Remove HTML tags from a javascript string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/14900469/stripping-html-code-from-an-item-in-an-array-using-jquery" class="question-hyperlink">Stripping HTML code from an item in an array using jQuery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/15494994/remove-html-tags-in-input-on-keypress" class="question-hyperlink">Remove HTML tags in &lt;input&gt; on keypress</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/15770757/javascript-count-characters-of-an-anchor-tag" class="question-hyperlink">Javascript - Count characters of an anchor tag</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/4521213/protect-against-script-injection-using-markdown" class="question-hyperlink">Protect against script injection using markdown</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/1879477/get-web-page-text-via-javascript" class="question-hyperlink">get web page text via javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-2</div></a> <a href="../../questions/17171139/how-to-strip-out-all-html-tags-from-string" class="question-hyperlink">How to strip out all html tags from string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/17493125/remove-invalid-html-tags-from-a-string" class="question-hyperlink">Remove invalid html tags from a string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/4536329/what-s-the-best-way-to-strip-out-only-the-anchor-html-tags-in-javascript-given" class="question-hyperlink">What's the best way to strip out only the anchor HTML tags in javascript, given a string of html?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/18480076/html-script-injection-via-javascript" class="question-hyperlink">HTML script injection via javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">32</div></a> <a href="../../questions/20365465/extract-text-from-html-while-preserving-block-level-element-newlines" class="question-hyperlink">Extract text from HTML while preserving block-level element newlines</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/7634664/remove-a-set-of-characters-from-a-script" class="question-hyperlink">remove a set of characters from a script</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">3</div></a> <a href="../../questions/22595181/javascript-get-plain-text-inside-a-span-which-has-other-nested-tags-present" class="question-hyperlink">Javascript: get plain text inside a span which has other nested tags present</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/22710520/removing-html-from-text" class="question-hyperlink">removing HTML from Text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">2</div></a> <a href="../../questions/22745725/disable-some-html-output-in-input-box" class="question-hyperlink">Disable some html output in input box</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">2</div></a> <a href="../../questions/22894602/ajax-validate-response-data-that-is-not-an-empty-page" class="question-hyperlink">Ajax validate response data that is not an empty page</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/22919114/should-be-easy-how-to-access-header-element-s-inner-text" class="question-hyperlink">should be easy: how to access &lt;header&gt; element's inner text?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/23409230/select-plain-text-in-between-tags" class="question-hyperlink">Select plain text in between tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">2</div></a> <a href="../../questions/4635297/how-to-remove-html-tags-from-textarea-with-javascript" class="question-hyperlink">How to remove HTML tags from textarea with JavaScript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/24593371/sort-array-of-html-based-on-output" class="question-hyperlink">Sort array of HTML based on output</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/25057842/prism-making-my-contenteditable-div-in-right-to-left" class="question-hyperlink">Prism making my contenteditable div in right-to-left</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">5</div></a> <a href="../../questions/25260752/remove-html-tag-from-a-string-using-jqquery" class="question-hyperlink">Remove html tag from a string using jQquery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">2</div></a> <a href="../../questions/25803100/how-do-i-get-the-li-text-alone-not-inner-html-too" class="question-hyperlink">How do i get the li text alone not inner HTML too</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-1</div></a> <a href="../../questions/25886591/trim-the-html-tags-and-character-entites-together-javascript-regular-expressio" class="question-hyperlink">Trim the HTML tags and character entites together Javascript Regular expression</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/26013890/compare-special-characters-with-variable-javascript" class="question-hyperlink">compare special characters with variable javascript</a> </div> </div> </div> <div class="module sidebar-linked"> <h4 id="h-linked">Related</h4> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/28440721/javascript-strip-html-tags-and-their-contents-from-a-string" class="question-hyperlink">Javascript - Strip HTML tags and their contents from a string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/30112631/remove-html-injection" class="question-hyperlink">Remove html injection?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">11</div></a> <a href="../../questions/31515999/how-to-remove-all-html-tags-from-a-string" class="question-hyperlink">How to remove all html tags from a string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-2</div></a> <a href="../../questions/33588514/how-to-remove-string-between-two-characters-every-time-they-occur" class="question-hyperlink">How to remove string between two characters every time they occur</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-3</div></a> <a href="../../questions/33666429/remove-all-html-with-regex-in-javascript-not-typical" class="question-hyperlink">Remove all HTML with Regex in Javascript (not typical)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/42557038/how-to-remove-html-code-with-javascript" class="question-hyperlink">How to remove HTML Code with Javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/47612472/how-to-remove-all-angle-brackets-and-everything-inside-using-js" class="question-hyperlink">How to remove all angle brackets and everything inside using JS?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/48661174/how-to-remove-html-text-attribute-tags-using-javascript" class="question-hyperlink">How to remove html text attribute tags using javascript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-2</div></a> <a href="../../questions/48826533/how-to-filter-out-html-tags-from-array-and-replace-with-nothing" class="question-hyperlink">How to filter out HTML tags from array and replace with nothing?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/52266692/how-to-get-rid-of-everything-inside-brackets-in-javascript" class="question-hyperlink">How to get rid of everything inside &lt;&gt; brackets in Javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">24</div></a> <a href="../../questions/51195143/is-there-a-way-to-remove-html-tags-from-a-string-in-javascript" class="question-hyperlink">Is there a way to remove html tags from a string in JavaScript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">309</div></a> <a href="../../questions/5002111/how-to-strip-html-tags-from-string-in-javascript" class="question-hyperlink">How to strip HTML tags from string in JavaScript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-3</div></a> <a href="../../questions/61617425/remove-everthing-between-two-characters" class="question-hyperlink">Remove everthing between two characters</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/63601163/remove-text-and-tag-in-string" class="question-hyperlink">Remove text and tag in string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">1</div></a> <a href="../../questions/65386359/nodejs-block-html-in-socket-io-messages" class="question-hyperlink">nodejs block html in socket.io messages</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/69949942/js-how-can-i-remove-text-under-conditions-of-starting-at-and-ending-at" class="question-hyperlink">JS, How can I remove text under conditions of starting at &lt; and ending at &gt;?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-1</div></a> <a href="../../questions/71950998/remove-from-varialble-of-html-codeblock-next-js" class="question-hyperlink">remove &quot;&quot; from varialble of html codeblock next js</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">0</div></a> <a href="../../questions/72200353/complex-search-and-replace-strings-with-javascript" class="question-hyperlink">complex search and replace strings with JavaScript</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/7116894/want-to-remove-any-html-tags" class="question-hyperlink">Want to remove any HTML tags</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/13446592/retrieving-values-from-a-string-using-jquery" class="question-hyperlink">Retrieving values from a string using jQuery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-3</div></a> <a href="../../questions/12941663/removing-html-tags-from-a-string-and-keeping-colon" class="question-hyperlink">Removing HTML tags from a string and keeping &quot;:&quot; (colon)</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">1</div></a> <a href="../../questions/14900469/stripping-html-code-from-an-item-in-an-array-using-jquery" class="question-hyperlink">Stripping HTML code from an item in an array using jQuery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">5</div></a> <a href="../../questions/2966796/how-to-get-a-webpage-as-plain-text-without-any-html-using-javascript" class="question-hyperlink">How to get a webpage as plain text without any html using javascript?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">28</div></a> <a href="../../questions/9727111/innerhtml-without-the-html-just-text" class="question-hyperlink">innerHTML without the html, just text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">2</div></a> <a href="../../questions/7540308/remove-html-tags-from-string-using-jquery" class="question-hyperlink">Remove html tags from string using jquery</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/7634664/remove-a-set-of-characters-from-a-script" class="question-hyperlink">remove a set of characters from a script</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">-2</div></a> <a href="../../questions/17171139/how-to-strip-out-all-html-tags-from-string" class="question-hyperlink">How to strip out all html tags from string</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/22710520/removing-html-from-text" class="question-hyperlink">removing HTML from Text</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/22919114/should-be-easy-how-to-access-header-element-s-inner-text" class="question-hyperlink">should be easy: how to access &lt;header&gt; element's inner text?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes default">-3</div></a> <a href="../../questions/23412171/how-can-i-fetch-text-from-element-only" class="question-hyperlink">How can i fetch text from element only</a> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>