[SOLVED] How to remove HTML tag (not a specific tag ) with content from a string in javascript

Issue

Is there any way to strip HMTL tag with content in HTML

example :

const regexForStripHTML = /(<([^>]+)>)/gi
const text = "OCEP <sup>&reg;</sup> water product"
const stripContent = text.replaceAll(regexForStripHTML, '')

output : 'OCEP &reg; water product'

I need to remove &reg; also from a string

expected output
OCEP water product

Solution

Removing all HTML tags and the innerText can be done with the following snippet. The Regexp captures the opening tag’s name, then matches all content between the opening and closing tags, then uses the captured tag name to match the closing tag.

const regexForStripHTML = /<([^</> ]+)[^<>]*?>[^<>]*?<\/\1> */gi;
const text = "OCEP <sup>&reg;</sup> water product";
const stripContent = text.replaceAll(regexForStripHTML, '');
console.log(text);
console.log(stripContent);

Answered By – phentnil

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

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