Strip HTML using JavaScript

A simple function that uses regular expressions to strip the HTML tags out of strings.
  1. <html>
  2. <head>
  3. <title>Strip HTML</title>
  4. <script type=“text/javascript”>
  5. String.prototype.stripHTML function()
  6. {
  7.  // What a tag looks like
  8.  var matchTag = /<(?:.|\s)*?>/g;
  9.  // Replace the tag
  10.  return this.replace(matchTag, “”);
  11. };
  12. function ShowStrippedText()
  13. {
  14.  var sourceInput = document.getElementById(“textSource”);
  15.  var destInput = document.getElementById(“textDestination”);
  16.  // Set the destination text area to the value of the newly stripped HTML
  17.         destInput.value = sourceInput.value.stripHTML();
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <textarea id=“textSource” rows=“10” cols=“80”><i>Some Sample <b>HTML</b> in here</i></textarea><br />
  23. <input type=“button” value=“Strip HTML” onclick=“ShowStrippedText()” /><br />
  24. <textarea id=“textDestination” rows=“10” cols=“80”></textarea>
  25. </body>
  26. </html>

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top