JAVASCRIPT

Remove HTML Tags from a String (Sanitization)

Learn to clean user-provided text by removing HTML tags using a simple yet effective JavaScript regex pattern, improving content security and display.

const removeHtmlTags = (htmlString) => {
  const htmlTagRegex = /<[^>]*>/g;
  return htmlString.replace(htmlTagRegex, "");
};

// Examples:
// const messyInput = "<h1>Welcome</h1><p>This is <strong>bold</strong> text.</p>";
// console.log(removeHtmlTags(messyInput)); // "WelcomeThis is bold text."
// const anotherInput = "Text with <script>alert('XSS');</script> evil script.";
// console.log(removeHtmlTags(anotherInput)); // "Text with  evil script."
How it works: This JavaScript function, `removeHtmlTags`, uses a regular expression to strip all HTML tags from a given string. The pattern `/<[^>]*>/g` identifies any sequence starting with `<` and ending with `>` (encompassing anything in between) and the `g` flag ensures all such occurrences are replaced. The `replace()` method then substitutes these matched HTML tags with an empty string, effectively removing them and returning a clean text output.

Need help integrating this into your project?

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

Hire DigitalCodeLabs