Home > Back-end >  How to delete a specific section of code in regex
How to delete a specific section of code in regex

Time:08-26

<div >
          <div >
            <a  target="_self" href="https://www.linkedin.com/in/abhijit-vinchurkar/details/experience?profileUrn=urn:li:fsd_profile:ACoAAAF3rCwBitUseeMU1pitSb2GHhRHT87b2pI">
<!---->      <span >
        Show all 6 experiences
      </span>
          </div>
      <div >

I want to delete lines from 'div ' till the end of div section using regex.

CodePudding user response:

To do only what you want, without handling the closing </div> tag, you could simply use this regex:

[ \t]*<div >[ \t]*\r?\n

It will search for spaces and tabs, zero or multiple times, then the div tag itself with one or more spaces before the class attribute. But then it does not handle if it has other attributes. It then also removes trailing spaces and the new line (Windows CRLF or Linux LF).

Test it here: https://regex101.com/r/Kh6zmJ/1

EDIT : question changed, you want to remove all the div tag with it's content

Regexp isn't the best way to solve it because if some children inside this div are also divs then we could match the wrong closing div. Better use a DOM parser. But well, you want a quick way to edit multiple files. So if you know the HTML code then ok, let's use a regexp.

It would be something like this:

[ \t]*<div >[\s\S]*?</?div>[ \t]*\r?\n

The idea is normally to use .*? to match anything in an ungreedy way until the first closing </div> tag. As VSCode doesn't handle the . to match also new lines with the s flag, you can replace . by [\s\S] which means any space or non-space character (so anything).

Your closing div tag seems to be wrong so I put an optional slash with the questionmark after it.

Test it here: https://regex101.com/r/LdvzHU/1

CodePudding user response:

In VSC

  • place cursor somewhere in tag <div >
  • execute command Emmet: Balance (outward)
  • Delete
  • Related