JAVASCRIPT
Remove HTML Tags from String using Regex in JavaScript
Learn to sanitize user input by effectively removing all HTML tags from a string using a regular expression in JavaScript to prevent XSS vulnerabilities.
function stripHtmlTags(htmlString) {
const htmlTagRegex = /<[^>]*>/g;
return htmlString.replace(htmlTagRegex, '');
}
// Examples:
// const messyHtml = "<h1>Hello</h1><p>This is <b>bold</b> text.</p>";
// console.log(stripHtmlTags(messyHtml)); // "HelloThis is bold text."
// const cleanText = "Just plain text.";
// console.log(stripHtmlTags(cleanText)); // "Just plain text."
How it works: This JavaScript function removes all HTML tags from a given string using a regular expression. The pattern `/<[^>]*>/g` matches any sequence that starts with `<`, contains zero or more characters that are not `>`, and ends with `>`. The `replace()` method then substitutes all found tags with an empty string, effectively sanitizing the input. This is crucial for preventing cross-site scripting (XSS) attacks when displaying user-generated content.