Sublime Text conditional find and replace
-
20-12-2019 - |
Question
It is possible to do a conditional find and replace in sublime text? I want to remove the external div class = "key-actions" but keep anything within them. The problem is the internal content will always be the same with maybe 3 different links at times
// this becomes
<div class="key-actions">
<a href="one-link.html"><div class="btn btn-primary btn-block text-left"><i class="icon-chevron-sign-right icon-showhouse2"></i> One Link</div></a>
<a href="two-link.html"><div class="btn btn-primary btn-block text-left"><i class="icon-chevron-sign-right icon-showhouse2"></i> Two Link</div></a>
</div>
// this
<a href="one-link.html"><div class="btn btn-primary btn-block text-left"><i class="icon-chevron-sign-right icon-showhouse2"></i> One Link</div></a>
<a href="two-link.html"><div class="btn btn-primary btn-block text-left"><i class="icon-chevron-sign-right icon-showhouse2"></i> Two Link</div></a>
Solution
This is kind of dependent on your code formatting, in particular the position of your new lines, but the following works for your example.
Find what:
(?s)<div class="key-actions">(.+?)\n</div>
Replace with:
\1
This will only work if there are no divs inside <div class="key-actions">
that close on a new line.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow