简体   繁体   中英

Is there a quick way to delete everything between certain tags, e.g. <head> and </head>, throughout the whole project (multiple pages) in VS Code?

I am trying to find a way to remove all from a tag pair in VS Code.

I've been using Notepad++ for this purpose, but for some unknown reason it doesn't work all the time. So, I hope if there is such a possibility in VS Code, it'd be more reliable. Here is the instruction for Notepad++: Search for - <wp:post_name>[^<>]+</wp:post_name> and replace all with - <wp:post_name></wp:post_name> Is there anything like this in VS Code? I'd really appreciate it if someone can help.

Before using what is suggested in this solution, backup your files, and run the search and replace on a small sample. Be sure to check the outcome to all the possible combinations you can have in your files.

You can achieve what you need with Notepad++ (and SublimeText 3, with RegEx search and replace), and this answer will cover that. Since I've never used Visual Studio Code, I can't say if it will work in it as well.


Consider the following regular expression.

<foo>(.*?)<\/foo>

If we were to apply it to the following text:

<foo><some special chars>!@#$%^&*</foo> sure, why not

<foo>Lorem</foo>

<foo>ipsum</foo>

<foo>sit</foo>

<foo>dolor</foo>

<foo>amet</foo>

<bar>elm stuff</bar>

more stuff for you  <foo> something </foo> and even more stuff <foo>yes</foo>

it would match all the parts of the text which begin with <foo> and end with </foo> , regardless of what's between them.

If you want to play around with this, I've created an example here .

As far as using this in Notepad++, open the search window, navigate to the Find in files tab, and set it up like in the following image.

打开搜索窗口的 Notepad++ 截图,正则表达式搜索设置

You would, of course, need to change the search and replacement strings to those you plan on using, optionally set up a file extension for which to do the replacement (Filters), and set the directory in which to perform find-and-replace.

Limitations

1. Nesting

In case your text contains nested tags of the same kind, like this:

Let's deal with nesting: <foo> some text <foo> a child foo!</foo> let's close the parent</foo>

doing the suggested RegEx search and replace, will turn the previous line of text into this:

Let's deal with nesting: <foo></foo> let's close the parent</foo>

If you don't have nested tags of the same kind, you should be in the clear. Unless...

2. Newlines

The provided RegEx will not match cases where your opening tag shows up in one line, and the closing tag shows up in another line. To match those, you would need to change the original RegEx:

<foo>(.*?)<\/foo>

to this:

<foo>([\s\S]*?)<\/foo>

\s will match any whitespace character (including newlines), while \S will match any non-whitespace character.

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