Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Find and Replace content between html tags using regex

I would like to use the equivalent of:

\<FooterTemplate.+?\<\/FooterTemplate\>/gms

In a Find and Replace regex for html/asp tags and their contents in visual studio 2015.
The above regex will not work in VS, but does in regex101.

The closest I got with VS syntax was something like:

\</*FooterTemplate[ ]*.*\> 

but only matches outside tags:

enter image description here


How can I edit the above regex to include contents in between the tag when searching?

like image 959
DonD Avatar asked Oct 26 '15 12:10

DonD


People also ask

How do you use regex in VSCode search?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

What regex does Visual Studio use?

Visual Studio uses . NET regular expressions to find and replace text.

How do you select similar tags in VSCode?

You can select a whole tag in VS Code by using the balance inward and balance outward Emmet commands. It's helpful to bind these commands to keyboard shortcuts, like Ctrl + Shift + Up Arrow for Balance Outward and Ctrl + Shift + Down Arrow for Balance Inward.

What flavor of regex does VSCode use?

VSCode is using JavaScript-based regex engine, but it is not the same. You cannot use named capturing groups there. There is no specific documentation on the regex used in VSCode. However, if you have a look at the source code, you will see lots of JS code around.


1 Answers

In VS search and replace tool, you can use [\r\s\S] character class to match any text spanning across multiple lines.

To match as few characters as possible to get a valid match, you need to use *? or +? lazy quantifier.

So, you can use

<FooterTemplate[\r\s\S]+?</FooterTemplate>
like image 171
Wiktor Stribiżew Avatar answered Nov 03 '22 00:11

Wiktor Stribiżew