Simple Regex to Remove Html tags

This is one of the simplest regex to remove html tags from some html text. I know its not the best but i’d argue that it’s one of the simplest. ;)


public static string RemoveHtml(string txt)
{
    return Regex.Replace(txt, @"<[^>]*>", "");
}

About these ads

2 Responses

  1. Just an FYI for those reading this. This regex does not take into account the possibility of a “>” symbol in an attribute within a tag.

    For example the following:
    1″ src=”test.jpg”>

    Would result in:
    1″ src =”test.jpg”>

  2. Sorry the markup was stripped from the previous comment using the technique described above. Subsequently I’ll repost again escaped:

    Just an FYI for those reading this. This regex does not take into account the possibility of a “>” symbol in an attribute within a tag.

    For example the following:
    <img alt="2 > 1" src="test.jpg">

    For example the following:
    1" src="test.jpg">

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: