开发者

Regex to remove withespaces from .aspx file

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:

  1. Whitespaces between strings enclosed between "" or '' should not be removed.
  2. 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.
  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜