Regex for Stripping HTML Tags From a String

Matches any HTML tag so it can be removed or replaced.

Published October 11, 2026

This regex matches HTML tags so you can remove them with a replace. It is fine for quick cleanup but is not a safe HTML sanitiser.

How the pattern works

Breaking the pattern down: < = opening angle bracket; [^>]* = anything except >; > = closing bracket.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/<[^>]*>/
✓ '<p>Hello</p>'
✓ '<br/>'
✗ 'no tags here'
✗ 'a > b'

Practical examples

The pattern

const re = /<[^>]*>/

Test it

const re = /<[^>]*>/
re.test('<p>Hello</p>')   // true
re.test('<br/>')   // true
re.test('no tags here')   // false
re.test('a > b')   // false

In Python

import re
re.search(r'<[^>]*>', text)

Common mistakes

  • Using regex for security sanitising (use DOMPurify).
  • Missing tags containing > in attributes.
  • Leaving script contents behind.

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

Is this safe against XSS?

No; use a proper sanitiser library.

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles