Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove HTML tags other than div and span from a string in JavaScript?

I need to remove from HTML line all tags except div and span. How can i do this ?

example

const str = "<div><p></p><span></span></div>";
 remove(str)//<div><span></span></div>
like image 305
Silicum Silium Avatar asked Jul 05 '21 03:07

Silicum Silium


People also ask

Which tag is used to remove all HTML tags from a string?

The strip_tags() function strips a string from HTML, XML, and PHP tags.

How do I get rid of HTML tags?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

Which function will remove the HTML tags from data?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

How to strip or remove HTML tags in JavaScript?

There are 4 common ways to strip or remove HTML tags in Javascript: Directly extract the text content from an HTML element – var txt = ELEMENT.textContent; Use a library such as String Strip HTML. That should cover the basics, but let us walk through more examples – Read on!

How to sanitize a string to make sure there are no HTML tags?

Sanitize a string to make sure there are no HTML tags? There are 4 common ways to strip or remove HTML tags in Javascript: Directly extract the text content from an HTML element – var txt = ELEMENT.textContent; Use a library such as String Strip HTML.

How to get the inner text/html of a Div?

If you assign an ID to the div, you can get the text using document.getElementById ('yourdivIDhere').innerHTML. Thanks for the suggestion, however its not a matter of getting the inner text/html, I want to remove the tags... You can do this.

Why is <Div> not parsing HTML content correctly?

HTML contained within <body> or <html> or <head> tags is not valid within a <div> and may therefore not be parsed correctly. textContent (the DOM standard property) and innerText (non-standard) properties are not identical. For example, textContent will include text within a <script> element while innerText will not (in most browsers).


Video Answer


2 Answers

To remove all tags excluding specific tags, you can use the following regular expression.

const str = "<div><p></p><span></span></div>";
console.log(str.replace(/(<\/?(?:span|div)[^>]*>)|<[^>]+>/ig, '$1'));
like image 101
s.kuznetsov Avatar answered Oct 16 '22 20:10

s.kuznetsov


You can use regex /<\/?(?!\bdiv\b)(?!\bspan\b)\b\w+\b>/g with replaceAll

let str = "<div><oz></oz><span></span><hjk></hjk></div>";

let str1 = str.replaceAll(/<\/?(?!\bdiv\b)(?!\bspan\b)\b\w+\b>/g,'')

console.log(str1);
like image 1
Vivek Bani Avatar answered Oct 16 '22 19:10

Vivek Bani