access_time 11 months ago languageEnglish

C# Regex - Remove Heading Tags

visibility 115 comment 0

C# regular expressions can be used to match and replace certain text patterns from a string variable.

Remove heading tags

The following regular expression can be used to remove all heading tags incl. h1 to h9 from HTML text string.


Code snippet

var html = "Your HTML string...";
var regex = new Regex(@"<[hH][1-9][^>]*>[^<]*</[hH][1-9]\s*>", RegexOptions.Compiled | RegexOptions.Multiline
var replacedHtml = regex.Replace(html, "");


Assuming the following is the input string:

<h3>Heading h3</h3>    
<h4>LINQ to SQL - Select N Random Records</h4>

After replacement, the output looks like the following:


Remove tags only

To keep all the text content but to remove all HTML tags, use the following regular expression:



For the above input HTML, the output looks like the following:

Heading h3    
LINQ to SQL - Select N Random Records

copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn.

Want to contribute on Kontext to help others?

Learn more

More from Kontext

visibility 1769
thumb_up 0
access_time 11 months ago
visibility 9400
thumb_up 0
access_time 7 years ago
C# 9.0 New Features
visibility 677
thumb_up 0
access_time 11 months ago