Here are two ways to strip out HTML tags from a string in JavaScript.
- Using the replace() method with a regular expression
- Using DOMParser API and the textContent property
Method 1: Using the replace() method with a regular expression
To strip out HTML tags from a string in JavaScript, you can use the “replace() method with a regular expression in combination with .textContent and .innerText properties from HTML DOM.”
<body>
<script>
function stripHTML(input) {
return input.replace(/<\/?[^>]+(>|$)/g, "");
}
// Usage:
var htmlString = "<div>Hello <span>World</span>!</div>";
console.log(stripHTML(htmlString));
</script>
</body>
Output
Method 2: Using DomParser API and the textContent property
To strip out HTML tags from a string in JavaScript, you can also “use a combination of the DOMParser API and the textContent property.”
<body>
<script>
function stripHTML(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent || "";
}
// Usage:
var htmlString = "<div>Hello <span>World</span>!</div>";
console.log(stripHTML(htmlString));
</script>
</body>
Output
However, it’s important to note that using the DOM to parse arbitrary HTML can be a security risk, especially if the HTML content comes from an untrusted source.
Always sanitize and validate data to prevent potential security issues. If you only deal with trusted content, these methods should work fine.
Regular expressions provide a concise way to remove HTML tags, but they may not handle all edge cases, especially with malformed HTML.
If you know the structure and source of your HTML content and are sure it’s consistent, this method can be very effective. Otherwise, for more robust parsing, the DOM-based methods are generally safer.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.