I'm trying to get Dreamweaver to remove all content between 2 html comment tags. I have a lot of html pages in my project that have a bunch of js scripts in the bottom and they all different from page to page.
For example, let's say I have the following html:
<!-- SCRIPTS -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="assets/js/custom.js"></script>
<!-- END SCRIPTS -->
And I'd like to do a Find and Replace that strips everything between the comment tags and replaces it with new stuff:
<!-- SCRIPTS -->
<script src="new-path-for-js-files.js"></script>
<!-- END SCRIPTS -->
I believe this could be possible to achieve by using regex but I couldn't find anything that would fit what I'm looking for.
I'd much appreciate your suggestions on accomplishing this.
Many thanks!
You can match the comment tags including the content they surround by using the following regex
:
<!-- SCRIPTS -->[\s\S]*?<!-- END SCRIPTS -->
Explanation:
The regex
looks for the exact text <!-- SCRIPTS -->
and <!-- END SCRIPTS -->
, and tries to match all characters in between:
\s
is the whitespace character class (containing any whitespace character like spaces, tabs, line breaks);
\S
will try to match anything that is not in the whitespace character class
[\s\S]
is a character set, which tries to mach any character it contains, so in this case that means any character;
*
is a quantifier which tries to match 0 or more of the preceding token [\s\S]
?
is a modifier which makes the preceding quantifier lazy: by default the a quantifier like *
is greedy and tries to match as many characters as possible, but the lazy modifier changes a quantifier so it tries to match as few characters as possible. This is necessary if you have multiple identical tags.
You said you want to replace the code inside the tags (so keeping the tags), which my suggested regex
does not do (it will also match - and thus replace - the comment tags). It is possible with a regex
using lookarounds, but that would make the regex
more difficult; and only part of the regex
flavors (partially) support lookarounds.
I suggest you simply add the comment tags around the replacement code if you want to keep them in the source code.