JAVASCRIPT
Remove Basic HTML Tags from a String
Discover a simple JavaScript regular expression to strip out common HTML tags from a string, useful for basic content sanitization or display purposes.
function stripHtmlTags(htmlString) {
const htmlTagRegex = /<[^>]*>/g;
return htmlString.replace(htmlTagRegex, '');
}
// Example
const messyHtml = "<p>This is some <b>bold</b> text.</p><script>alert('xss');</script><span>Hello</span>";
console.log(stripHtmlTags(messyHtml)); // "This is some bold text.alert('xss');Hello"
How it works: This JavaScript function `stripHtmlTags` uses a regular expression to remove all content enclosed within angle brackets (`<`, `>`), effectively stripping out most HTML tags from a string. It's a basic sanitization method for display but should not be solely relied upon for robust security against XSS.