R

TestRegex

← Back to Blog

How to Match HTML Tags (Safely)

The golden rule of the internet: Do not use regex to parse HTML. Use a proper DOM parser like Cheerio or BeautifulSoup.

The Exception to the Rule

Sometimes you just need to strip tags from a string to count words or generate a snippet.

<[^>]+>

This matches an opening bracket, anything that isn't a closing bracket, and then a closing bracket. It's fast and "good enough" for non-security contexts.

Security Warning

Never use regex to sanitize input against XSS. It is trivial to bypass regex filters with malformed HTML that browsers still execute.