Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex: HTML tags
Regex pattern · find & extract

Regex: HTML tags

Regex to remove HTML tags: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /<[^>]+>/g with the global flag to find every match. In "<p>Hello <b>world</b></p>" it finds "<p>", "<b>", "</b>", "</p>". Regex cannot parse HTML reliably (comments, scripts, > inside attributes).

Last updated · Maintained by FreeAIToolz · How we test

/<[^>]+>/g

4 match(es)

<p>Hello <b>world</b></p> No tags here
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
<opening angle bracket
[^>]+anything that is not a closing bracket
>closing angle bracket

Flags: g finds every match instead of stopping at the first.

Examples

InputMatches
<p>Hello <b>world</b></p><p>, <b>, </b>, </p>
No tags herenone

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Regex cannot parse HTML reliably (comments, scripts, > inside attributes). For anything beyond quick cleanup, use DOMParser in the browser or an HTML parser such as cheerio or BeautifulSoup. Never use it to sanitise HTML for security.

When to use this pattern

Stripping tags from simple HTML snippets to get plain text (replace matches with "").

Use it in your language

JavaScript

const matches = text.match(/<[^>]+>/g) ?? [];

Python

import re
matches = [m.group(0) for m in re.finditer(r'<[^>]+>', text)]

PHP

preg_match_all('~<[^>]+>~', $text, $m);
$matches = $m[0];

Java

Matcher m = Pattern.compile("<[^>]+>").matcher(text);
while (m.find()) { System.out.println(m.group()); }

Go

re := regexp.MustCompile(`<[^>]+>`)
matches := re.FindAllString(text, -1)

More regex patterns

Frequently asked questions

What is the regex for HTML tags?
/<[^>]+>/g. Regex cannot parse HTML reliably (comments, scripts, > inside attributes). For anything beyond quick cleanup, use DOMParser in the browser or an HTML parser such as cheerio or BeautifulSoup. Never use it to sanitise HTML for security.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
How do I replace the matches?
In JavaScript use text.replace(/<[^>]+>/g, "replacement"); in Python use re.sub(r'<[^>]+>', "replacement", text).