I'm looking for a Regex that would remove all the white spaces from html code present in .aspx file. Here are some of the conditions that the Regex should satisfy:
- Whitespaces between strings enclosed between "" or '' should not be removed.
- Whitespaces between html attributes/tags, for example like
<a href>text1 text2</a>
, should not be removed. Html can also start with<!
in case of doctype specification. In short, only Whitespaces that appear after a html tag needs to be removed. - Whitespaces between server tags enclos开发者_开发知识库ed between <% %>, should not be removed.
If you would be able to provide one single Regex that satisfies all the above condition, it would be great. Otherwise, separate Regex is also fine.
Thanks in advance.
A naïve but simple regex trick to do this is to match the whitespaces between >
and <
.
That'll keep the tags and their text content untouched.
In javascript (js doesn't support lookbehinds):
- Pattern:
>\s+(?=<)
- Replace with:
>
In a regex engine that supports lookbehinds (f.e. PCRE):
- Pattern:
(?<=>)\s+(?=<)
- Replace with nothing
A test here
精彩评论