JAVASCRIPT
Basic HTML Tag Stripping from Text
Safely remove common HTML tags from a string using a simple regex pattern, useful for cleaning content or preventing basic XSS issues when displaying text.
function stripHtmlTags(htmlString) {
// Regex to match any HTML tag: < followed by any characters not > (non-greedy), followed by >
// The `g` flag ensures all occurrences are replaced.
return htmlString.replace(/<[^>]*?>/g, '');
}
// Examples
console.log(stripHtmlTags("This is a <b>bold</b> and <i>italic</i> text.")); // "This is a bold and italic text."
console.log(stripHtmlTags("<p>Hello <a href='#'>World</a>!</p>")); // "Hello World!"
console.log(stripHtmlTags("Text with <img src='x.png' alt='image'> embedded.")); // "Text with embedded."
console.log(stripHtmlTags("No tags here.")); // "No tags here."
How it works: This function provides a straightforward way to remove HTML tags from a string using a regular expression. The pattern `/<[^>]*?>/g` matches any sequence that starts with an opening angle bracket (`<`), contains any character except a closing angle bracket (`>`) zero or more times (non-greedily due to `*?`), and ends with a closing angle bracket (`>`). The `g` flag ensures that all instances of HTML tags are found and replaced with an empty string, effectively stripping them from the text. This is useful for producing plain text output from HTML content.