Wednesday, August 7, 2013

How to use RegEx in c# to remove or replace HTML characters

If you need to remove HTML tags from string you can use reqular expression to do that.
For Example you get the following code from client side "<p>Hi, How are you?</p><div>I'm fine.</div>".


using System.Text.RegularExpressions;


public static string HtmlToString(string HTMLText)
{
  Regex reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);

  return reg.Replace(HTMLText, "");
}



The output of the method HtmlToString will be: "Hi, How are you?I'am fine"

No comments:

Post a Comment