Regex to match all the HTML tags only, not the text

For many reasons we want just text from paragraphs and divs, but all the html tags extracted. In this tutorial, we are showing you the best regex that only match html tags from the content. We also covered various situations and problem you may face during this regex.

If you have a large content that contains many html tags that you can’t define then use below Regex. It will select each html tag starts from < and ending at >.

The regex to match all html tags only but not the text is as follows –


Check on Regex 101

This is one of my favorite regex, I need this regex many times.

As you see, we didn’t added p tag in the regex, but it select p html opening and closing tag because it is enclosed between < & >.

Above regex is best to select all the html tags, but if you want to select a specific html tag and not other one, the you need to put that tag in above regex with little change, given below.

Lets suppose, you have a content including p tag & div tag, and you only want to select p tag and not the div tag, then regex should be like this,


Check on Regex 101

we have only made a small change by adding p at the end of the previous regex, so now it will only select p tag. You can change that p tag to any tag you want to select

Hope you have found the best solution for the problem. If you remained with any doubt, please ask in the comment section. And if you have another question about regex ask here.

Leave a Comment