3

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>
2
  • 3
    You should add minimal reproducible code. Commented Jul 5, 2021 at 3:40
  • Are you trying to remove the tags or the HTML nodes? Commented Jul 5, 2021 at 17:28

3 Answers 3

2

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'));

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't delete tags with the same start. For example, if you write (?:s|div), it won't delete span tags.
1

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);

Comments

0

One option would be to use DomParser's parseFromString() method to parse the string as (temporary) HTML. You can then delete the target elements with .remove(), and finally convert back to a string with toString() if required:

const str = "<div><p></p><span></span></div>";

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(str, 'text/html').body;

var spans = htmlDoc.getElementsByTagName('span');
for (var i = 0; i < spans.length; i++) {
  spans[i].remove();
}

var parsed = htmlDoc.innerHTML.toString();
console.log(parsed);

1 Comment

Careful with the for loop. getElementsByTagName returns a live collection. See stackoverflow.com/questions/23988982/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.