简体   繁体   中英

How to remove all instances of an HTML tag VS Code

I have an HTML file that has many useless tags like empty <p> tags, and <span> 's that I want to delete. Is there any easy way to remove all <span> 's from my document without finding each one and deleting it?

Yes. You can use a regex to highlight all the empty elements and then just replace them with nothing. The regex is <[^/>][^>]*>\s*\n*<\/[^>]+> , which includes empty tags that span multiple lines.

In VSC, when you do a search and replace ( CTRL + H ), make sure you have the "Use Regular Expression" option selected ( Alt + R ), use the above regex, and then replace it with nothing.

Unfortunately, I don't think that there's any package or an extension that can help you with it, however, what I personally usually do in cases like such: highlight one of the tags that I need removed, reach out to Select all occurrences of current word shortcut in VS Code:

  • for Mac it's Cmd + F2 or fn + Cmd + F2 on the highlighted word
  • for Windows its Ctrl + F2

and with Cmd + Backspace remove all lines with the highlighted tags.

Hit the magnifying glass icon on the left side of your screen, type in whatever you want to delete all of, and then set it to be replaced with "", then select replace all. 在此处输入图像描述

I have no results, so I can't replace all, but you hopefully get the idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM