remove html tags from string javascript – How to Remove HTML Tags from String in Javascript?

remove html tags from string javascript 4 Ways to Strip & Remove HTML Tags USING REGULAR EXPRESSION, TEXT CONTENT, DOM PARSER & TEXT CONTENT and USING STRIP HTML LIBRARY.

remove html tags from string javascript

We can remove HTML/XML tags in a string using regular expressions in javascript.

Use JavaScript replace() method

string.replace(/<\/?[^>]+(>|$)/g, "")

remove all html tags from string javascript

userInput.replace(/<[^>]*>?/gm, '');

don’t miss : How To Remove Html Tags From String In Php?

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>How to Remove HTML Tags from String in Javascript? - www.pakainfo.com</title>
</head>
<body>
    
    <div>        
        <button>Remove/Delete Tags</button>
        <p id="clst_first"></p>
        <p id="clst_two"></p>
    </div>
    
    <script src="script.js"></script>
</body>
</html>

JavaScript Code

let btnRemove = document.querySelector('button');
let clst_first = document.querySelector('#clst_first');
let clst_two = document.querySelector('#clst_two');

let userInput = `<p>Please <u>remove</u> all <strong>HTML</strong> tags <em>from this</em> string.`;


window.addEventListener('DOMContentLoaded', () => {
   clst_first.innerHTML = userInput;
})

btnRemove.addEventListener('click', () => {
   let parser = new DOMParser();
   let doc = parser.parseFromString(userInput, "text/html");
   clst_two.textContent = doc.body.textContent || "No Content";
});

CSS code

div {
  text-align: center;
}

button {
  padding: 10px 20px;
}

p {
  margin-top: 20px;
}

javascript remove html from text

remove html tags from a string, leaving only the inner text

function removeHTML(str){ 
    var dmp_data = document.createElement("DIV");
    dmp_data.innerHTML = str;
    return dmp_data.textContent || dmp_data.innerText || "";
}
var html = "<div>Paka Paka Information!</div>";
var onlyText = removeHTML(html); "Paka Paka Information!"

Source Code example to strip tags from HTML string in js code.

var str = "<p>Welcome, <b>Pakainfo</b></p>";
var cleanText = str.replace(/<\/?[^>]+(>|$)/g, "");

I hope you get an idea about remove html tags from string javascript.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.