How to strip HTML tags using regular expressions in .NET?

I use following method to make short descriptions from HTML content.

It strips HTML tags and takes specified length from content.

public static string Ellipsis(this string text, int length)

{

    string s = Regex.Replace(text, @"\<[^\>]*\>", "");

    if (lenght > 3 && lenght < s.Length)

        if (lenght - 3 < s.Length)

            return s.Substring(0, lenght - 3) + "...";

    return s;

}


Posted on Wednesday, March 19, 2008 by | Comments (2) | Add Comment

Comments

Gravatar

Re:How to strip HTML tags using regular expressions in .NET?

Not enough information

Posted on 6/10/2010 2:51:48 AM by Ashish kumar #
Gravatar

Re:How to strip HTML tags using regular expressions in .NET?

good one

Posted on 11/28/2011 1:14:45 AM by fize #

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5