JAVASCRIPT
Removing Basic HTML Tags from Text
Perform basic sanitization by stripping common HTML tags from a string using a JavaScript regular expression, useful for cleaning text content.
const stripHtmlTags = (htmlString) => {
// This regex is for basic tag removal and should not be used for robust XSS prevention.
// It removes anything that looks like <tag> or </tag>
return htmlString.replace(/<[^>]*>/g, '');
};
const maliciousHtml = "Hello <script>alert('XSS!');</script> World! <p>This is safe.</p>";
console.log(stripHtmlTags(maliciousHtml));
// Expected: "Hello World! This is safe."
How it works: The `stripHtmlTags` function uses the regular expression `/<[^>]*>/g` to find and remove any sequence of characters starting with `<` and ending with `>`, effectively stripping HTML tags. It's important to note this is a basic sanitization technique suitable for simple text cleaning, but not a comprehensive solution for preventing advanced XSS attacks.