JAVASCRIPT
Strip Basic HTML Tags from a String
Effectively remove simple HTML tags from a string using a regular expression in JavaScript, useful for basic content sanitization or displaying plain text.
function stripHtmlTags(htmlString) {
return htmlString.replace(/<[^>]*>/g, '');
}
// Example usage:
const dirtyHtml = "<h1>Title</h1><p>Some <b>bold</b> text.</p>";
console.log(stripHtmlTags(dirtyHtml)); // "TitleSome bold text."
const anotherHtml = "User input with <script>alert('xss');</script> evil code.";
console.log(stripHtmlTags(anotherHtml)); // "User input with evil code."
const malformedHtml = "<div unclosed tag";
console.log(stripHtmlTags(malformedHtml)); // ""
How it works: The `stripHtmlTags` JavaScript function uses a simple regular expression to remove all HTML tags from a given string. It finds any sequence starting with '<' and ending with '>', regardless of content, and replaces it with an empty string, providing a basic way to extract plain text from HTML content. Note that this is a simple approach and not a full security solution for XSS.