Regular Expression for Removing HTML Tags

Posted on
By Addam Driver

Just incase anyone ever needs to remove HTML Tags in some string but want’s to keep the content in-between them. /(<([^>]+)>)/ig

An example would be something like this for javaScript:



var htmlText = "<h1><span>Get these tags away from me!</span></h1>";
var noHTML = /(<([^>]+)>)/ig; // regex for removing HTML tags
var clean = htmlText.replace(noHTML, ''); // remove all html tags
console.log(clean); // will return "Get these tags away from me!"


I hope this helps someone out there!

-A-

Tags: , , , , , , , ,

Comments

    • Howard says:

      I guess markup in the post doesn’t work properly. Try embedding an SGML comment, or an give an element an attribute with an embedded left or right angle-bracket.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CSS Methods

HTML Goodies

JavaScript

mustache

node.js

The HTML Writers Guild IWA