regex to remove html tags javascript code example

Example 1: javascript strip html tags from string

var htmlString= "<div>\n <h1>Hello World</h1>\n <p>This is the text that we should get.</p>\n <p>Our Code World &#169; 2017</p>\n </div>";

var stripedHtml = htmlString.replace(/<[^>]+>/g, '');
var decodedStripedHtml = he.decode(stripedHtml);

// Hello World
// This is the text that we should get.
// Our Code World &#169; 2017
console.log(stripedHtml);

// Hello World
// This is the text that we should get.
// Our Code World © 2017
console.log(decodedStripedHtml);

Example 2: regex remove html tags

const s = "<h1>Remove all <b>html tags</n></h1>"
s.replace(new RegExp('<[^>]*>', 'g'), '')

Example 3: regex remove html tags

String target = someString.replaceAll("<[^>]*>", "");

Tags:

Html Example