JAVASCRIPT

Strip HTML Tags from String using Regex

Discover a simple yet effective regular expression in JavaScript to remove all HTML tags from a string, useful for sanitizing user input or displaying plain text.

function stripHtmlTags(htmlString) {
  // Regex to match any HTML tag: <tagname attributes>content</tagname>
  // The /g flag ensures all occurrences are replaced.
  const htmlTagRegex = new RegExp(/<[^>]*>/g);
  return htmlString.replace(htmlTagRegex, "");
}

// Usage examples:
// const richText = "<h1>Hello World!</h1><p>This is <strong>bold</strong> text.</p>";
// console.log(stripHtmlTags(richText)); // "Hello World!This is bold text."
// console.log(stripHtmlTags("<a href='#'>Link</a> without attributes.")); // "Link without attributes."
How it works: The `stripHtmlTags` function in JavaScript uses a regular expression to remove all HTML tags from a given string. The pattern `/<[^>]*>/g` matches any sequence that starts with '<', contains any characters that are not '>', and ends with '>'. The `g` (global) flag ensures that all matched tags, not just the first one, are replaced with an empty string, effectively stripping them from the text. This is useful for sanitizing user-generated content or displaying plain text versions of 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