Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:54:35+00:00 2026-05-28T02:54:35+00:00

On ASP.NET MVC 3, I created a Action Filter for white space removal from

  • 0

On ASP.NET MVC 3, I created a Action Filter for white space removal from the entire html. It works as I expected most of the time but now I need to change the RegEx in order not to touch inside pre element.

I get the RegEx logic from awesome Mads Kristensen‘s blog and I am not sure how to modify it for this purpose.

Here is the logic:

public override void Write(byte[] buffer, int offset, int count) {

    string HTML = Encoding.UTF8.GetString(buffer, offset, count);

    Regex reg = new Regex(@"(?<=[^])\t{2,}|(?<=[>])\s{2,}(?=[<])|(?<=[>])\s{2,11}(?=[<])|(?=[\n])\s{2,}");
    HTML = reg.Replace(HTML, string.Empty);

    buffer = System.Text.Encoding.UTF8.GetBytes(HTML);
    this.Base.Write(buffer, 0, buffer.Length);
}

Whole code of the filter:

https://github.com/tugberkugurlu/MvcBloggy/blob/master/src/MvcBloggy.Web/Application/ActionFilters/RemoveWhitespacesAttribute.cs

Any idea?

EDIT:

BIG NOTE:

My intention is totally not speed up the response time. In fact,
maybe this slows things down. I GZiped the pages and this minification makes me
gain approx 4 – 5 kb per page which is nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:54:35+00:00Added an answer on May 28, 2026 at 2:54 am

    Parsing HTML with regex very complicated and any simple solutions could break easily. (Use the right tool for the job.) That being said I’ll show a simple solution.

    First I simplified the regex you had to:

    (?<=\s)\s+
    

    Replace those matches with an empty string to get rid of double spaces everywhere.

    Assuming there are no < or > inside the pre tag, you can add (?![^<>]*</pre>) at the end of the expression to make it fail inside of pre tags. This makes sure that </pre> doesn’t follow current match, without any tags in between.

    Resulting in:

    (?<=\s)\s+(?![^<>]*</pre>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you have an ASP.Net MVC form created by Html.BeginForm(), how do fields inside
If I create an object in a Custom Action Filter in ASP.NET MVC in
In ASP.NET MVC 2, to secure controller action, i have created a class RequirePermission
After reading ASP.NET MVC 2 in Action and watching Jimmy Bogard's presentation from MvcConf
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.
I created MVC ASP.Net Web application and tried insert Thai language data to SQL
I just created a ASP.NET MVC project in VS2008. When I press F5 to
I just created a ASP.NET MVC project and was able to build and browse
I have created firstly ASP.NET MVC 2 . and write more functionality. After I
I created a new ASP.NET MVC application. The home page looks like this: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.