1

I'm trying to remove entire tag with specific class:

Code:

<div> 
Blah blah 
<div class='myClass'>
  <div>some other div</div>
</div>
</div>

<div> 
Blah blah 
<div class='myClass'>
  more text
</div>
</div>

<div> 
Blah blah 
<div class='myClass'>
  more text
</div>
</div>

I want to search for .myClass tag and replace it with "" (delete)
so the Result is:

<div> 
Blah blah 

</div>

<div> 
Blah blah 

</div>

<div> 
Blah blah 

</div>

I know it's easy with jQuery but I want to do it on the text editor side. Or maybe I can just view code and copy it after jquery has deleted it. Sublime Text or Notepad++

Thanks!

2 Answers 2

3

Use notepad++ with regular expressions

  1. Open Notepadd++
  2. Press CTRL+H
  3. Select Regular expression as search mode
  4. Enter ^.*myClass.*$ under Find what and nothing under Replace with
  5. Click Replace all

enter image description here


Cases where start and end strings are in different lines (Notepad 6.xx)

We use a workaround and temporary delete all carriage returns (+newlines).
This gives us a single long line where its much easier to use RegEx. Later we put the carriage returns back.

  1. Choose a unique string like %%%NEWLINE%%% which doesn't occur so far in your text.
    Test it first with a simple CTRL+F search
  2. Press CTRL+H and select Regular expression as search mode
  3. Find \r\n and replace with %%%NEWLINE%%%. Click Replace all.
    \r\n are special characters and stand for carriage return and newline. You get one long line.
    Remember: From now on you can't use ^ and $ anymore
  4. Find <div .*?myClass.*?</div> and replace with nothing (Notice the space)
  5. Find %%%NEWLINE%%% and replace with \r\n to bring back our normal text structure

While writing my edit I noticed you have changed your question with a game breaker. This method (probably no RegEx method at all) won't work while you have nested <div></div> tags. The RegEx engine can't know if it should stop at the 2nd, 3th or n-th </div>

Used RegEx

^  → line beginning
.  → any single character
*  → repeat previous pattern 
.*  → any characters as long as possible (greedy)  
.*? → any characters as short as possible (non greedy)
$  → line end

Used resources

4
  • Works great on one line. I failed to propery simplify the example. Sorry. I've edited it so the closing </div> is on a different line.
    – Miro
    Jul 22, 2013 at 19:38
  • Your last edit gave me some headache. Nested <div> tags are a problem for my RegEx method.
    – nixda
    Jul 22, 2013 at 21:13
  • 1
    Sorry abou that :) You went above and beyond! I really think text editors should have a function or plugin for selecting html tags and classes (non-RegEx based :) ).
    – Miro
    Jul 22, 2013 at 21:24
  • For a single operation, there is a notepad++ plugin called HTML tag. Here is a example picture. This only helps to identify a closing tag. Nothing more, sorry.
    – nixda
    Jul 22, 2013 at 22:36
0

I still use Dreamweaver mostly because its "specific tag" search. You can find, remove, strip and change everything easily. On your case you can find every div tag that has myClass class and remove tag with content. It only takes 5 sec to have your wanted result.

You can see how it works on this link http://www.lehigh.edu/computing/web/IntDreamweaver/find.html

This is the most important missing part on notepad++ for my needs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .