Sign in
Log inSign up

Remove specific tag from string.

Rushi Jagani's photo
Rushi Jagani
·Apr 22, 2019
var data = '<p><span class="unwanted-span-one" data-hello="unwated-attr">Donec</span> rutrum congue leo eget malesuada. <span>Mauris</span> blandit aliquet elit, eget <span class="unwanted-span-two" data-hello="unwated-attr-two"> malesuada</span>.</p>'

I want to remove all span tag which has a class name start with "unwanted-span-", it should remove only the tag wrap not inside content.

so the desired output should be as follow

var data = <p>Donec rutrum congue leo eget malesuada. <span>Mauris</span> blandit aliquet elit, eget malesuada.</p>'

Let me know if it's possible or not. If yes can you please help me with that.

Please note -- we only need to remove span tags with class name starts "unwanted-span-" other span should remain as it is.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct