Clear Html Tags from a result of an input, except

1

How do I remove all html tags from a string with exception? Ex:

var exemplo = "<div><p>Leno<span> Sousa</span> <i></i>var exemplo = "<div><p>Leno<span> Sousa</span></p></div>";var exemplo = "<div><p>Leno<span> Sousa</span></p></div>";></div>";

and return as:

var exemplo = "<p>Leno Sousa</p>";

As you can see, it cleared the tags and only the 'p' tag remained.

    
asked by anonymous 23.05.2018 / 17:46

1 answer

1

You can use a regex with .replace that removes all tags (pure JavaScript).

I created a function that takes two parameters: the string and the tag that will stay, and returns only the text inside the specified tag:

function removeTags(string, excecao){
   return excecao + string.replace(/(<([^>]+)>)/g, "").trim() + excecao.replace("<", "</");
}

var exemplo = "<div><p>Leno<span> Sousa</span> <i></i>";

var filtrado = removeTags(exemplo, "<p>");
console.log(filtrado);
    
23.05.2018 / 18:42