Adsence750x90

Saturday, August 1, 2009

How to Strip (Remove) HTML tag from a String Using ASP.NET C# - using regular expression

How to Strip (Remove) HTML tag from a String Using ASP.NET C#

Using regular Expressions we can strip HTML Contents from a string. System.Text.RegularExpressions namespace conatin regular expressions in asp.net.
Replaces all occurrences of a pattern defined by a specified regular expressionwith a specified replacement character string, starting at the first character in the input string. Options can be specified to modify matching behavior.

Syntax

1: Regex.Replace(string input, string pattern, string replacement);
2: Regex.Replace(string input, string replacement, int count, int startat);
3: Regex.Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options);

Example

string pattern = @"<(.|\n)*?>";
string withOutHtml=Regex.Replace("html content",pattern,string.Empty,RegexOptions options);

3 comments:

Vipul said...

It does'nt replace img tag. What to do to replace image tag?

Raju.M said...

vipul it replace all the html part of the given string.i tested this after that i posted this code snippet.

dont forget to add this on ur code using System.Text.RegularExpressions;

Raju.M said...

please check this image http://cafekerala.com/makhaaiblogspot/regex.jpg