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>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top