JAVASCRIPT

Remove Basic HTML Tags from Text

Learn to strip common HTML tags from a string using a regular expression in JavaScript for basic content cleaning or plain text display purposes.

function removeHtmlTags(htmlString) {
  // This regex matches any HTML tag (e.g., <p>, <div>, <a>, <img>)
  // It's a basic removal and not a robust XSS prevention mechanism.
  const tagRegex = /<[^>]*>/g;
  return htmlString.replace(tagRegex, '');
}

// Example usage:
const richText = "<p>This is some <strong>rich</strong> text with a <a href='#'>link</a>.</p>";
console.log(removeHtmlTags(richText)); // "This is some rich text with a link."

const maliciousText = "Hello <script>alert('XSS');</script> user!";
console.log(removeHtmlTags(maliciousText)); // "Hello  user!"
How it works: The `removeHtmlTags` function employs a regular expression `/<[^>]*>/g` to identify and remove all basic HTML tags from a given string. This method is useful for quickly cleaning up content, for example, to extract plain text from rich text inputs before displaying it in contexts that don't support HTML. It's important to understand that this is a basic cleanup and not a comprehensive solution for preventing Cross-Site Scripting (XSS) vulnerabilities; for robust security, dedicated server-side sanitization libraries are recommended.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs