JAVASCRIPT

Sanitize Text by Removing HTML Tags

Effectively remove all HTML tags from a string using a simple regular expression in JavaScript, useful for content sanitization.

function stripHtmlTags(htmlString) {
  return htmlString.replace(/<[^>]*>/g, '');
}

const rawHtml = "<h1>Hello, World!</h1><p>This is a <b>test</b> paragraph.</p>";
const cleanText = stripHtmlTags(rawHtml);
console.log(cleanText); // "Hello, World!This is a test paragraph."

const unsafeHtml = "<script>alert('XSS');</script>Buy <em>now</em>!";
const sanitizedText = stripHtmlTags(unsafeHtml);
console.log(sanitizedText); // "Buy now!"
How it works: This snippet provides a function `stripHtmlTags` that uses a regular expression to remove all HTML tags from a given string. The pattern `/<[^>]*>/g` matches any sequence starting with `<` and ending with `>`, effectively targeting and replacing all tag structures with an empty string. This is a common technique for basic content sanitization or extracting plain text from rich HTML.

Need help integrating this into your project?

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

Hire DigitalCodeLabs