JAVASCRIPT

Removing Basic HTML Tags from a String

A JavaScript regex snippet to strip simple HTML tags from a string, useful for converting rich text to plain text or basic content sanitization.

const stripHtmlTags = (htmlString) => {
  // This regex matches any HTML tag (e.g., <p>, <div>, <a>, etc.)
  // It's a simple approach and might not handle complex or malformed HTML perfectly.
  const htmlTagRegex = /<[^>]*>/g;
  return htmlString.replace(htmlTagRegex, '');
};

const richText = "<p>Hello, <strong>world</strong>!</p><span>This is a test.</span>";
console.log(stripHtmlTags(richText)); // "Hello, world!This is a test."

const moreHtml = "<div><a href='#'>Link</a> with some text.</div>";
console.log(stripHtmlTags(moreHtml)); // "Link with some text."
How it works: The `stripHtmlTags` function uses a simple regular expression `/<[^>]*>/g` to find and replace all occurrences of HTML tags within a given string with an empty string. This effectively removes tags like `<p>`, `<div>`, `<strong>`, etc., leaving only the plain text content. While effective for basic sanitization and extracting text, it's important to note that this method is not foolproof for deeply nested or maliciously malformed HTML.

Need help integrating this into your project?

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

Hire DigitalCodeLabs