Here are 2 ways to strip HTML code from a given text.
1. Use DOMParser
const stripHtml = function (html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || '';
};
2. Use template
The <template>
tag holds a HTML content that is not to be rendered immediately. However, this is not supported on older browser such as IE 11.
const stripHtml = function (html) {
const ele = document.createElement('template');
ele.innerHTML = html;
return ele.content.textContent || '';
};