★ Web · regex
Regex for HTML Tag
Finds things that look like HTML tags in text, capturing the tag name. Useful for quick scans and stripping simple markup. It can't understand nesting, comments, scripts or attributes containing >.
- Runs in your browser
- No signup
Try the html tag pattern
Read it in plain English (best-effort)
- <the character '<'
- /the character '/'
- ?the previous is optional
- (start capture group 1
- [A-Za-z]one character from: A–Z, a–z
- [A-Za-z0-9-]one character from: A–Z, a–z, 0–9, '-'
- *zero or more of the previous
- )end of group
- \ba word boundary
- [^<>]any one character except '<', '>'
- *zero or more of the previous
- >the character '>'
One sample per line works well with the m flag on anchored patterns.
The pattern, piece by piece
/<\/?([A-Za-z][A-Za-z0-9-]*)\b[^<>]*>/g| Part | What it does |
|---|---|
< | an opening angle bracket |
\/? | an optional slash for closing tags |
([A-Za-z][A-Za-z0-9-]*) | group 1: the tag name, like div or my-component |
\b | the name ends here |
[^<>]* | attributes: anything except angle brackets |
> | the closing bracket |
This pattern isn't anchored with ^…$, because its job is to find matches inside longer text, not to validate a whole value.
What it accepts and rejects
Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.
| Should match | |
|---|---|
<div> | ✓ |
</p> | ✓ |
<a href="/x">link</a> | ✓ |
<img src="a.png" /> | ✓ |
<my-component prop=1> | ✓ |
text <br> more | ✓ |
| Should not match | |
|---|---|
plain text | ✗ |
a < b and c > d | ✗ |
<> | ✗ |
< div> | ✗ |
<1abc> | ✗ |
5 > 3 < 4 | ✗ |
Copy it into your code
RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.
const htmlTagRe = /<\/?([A-Za-z][A-Za-z0-9-]*)\b[^<>]*>/g;
export function findHtmlTags(text) {
return [...text.matchAll(htmlTagRe)].map((m) => m[0]);
}When not to use regex for this
Never parse or sanitise HTML with regex. Use DOMParser in the browser, a real parser on the server (parse5, BeautifulSoup, golang.org/x/net/html), and a sanitiser like DOMPurify for anything user-supplied. Regex-based sanitising is a classic XSS hole.
Questions engineers actually ask
Can I use this to strip HTML tags?
Why doesn't it match <!-- comments -->?
What is catastrophic backtracking?
More web patterns
- Email AddressPragmatic email check: catches typos without rejecting real addresses.^[A-Za-z0-9_%+-]+(?:\.[A-Za-z0-9_%+-]+)*@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*\.[A-Za-z]{2,}$
- URLhttp(s) URLs with a real domain, optional port, path, query and fragment.^https?:\/\/(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}(?::\d{1,5})?(?:[\/?#]\S*)?$
- Domain NameHostnames like example.com or sub.example.co.uk, including punycode.^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$
- URL SlugLowercase kebab-case slugs like my-first-post.^[a-z0-9]+(?:-[a-z0-9]+)*$
- IPv4 AddressDotted-quad IPv4 with each octet 0–255 and no leading zeros.^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$
- IPv6 AddressFull and compressed (::) IPv6 addresses.^(?:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}|:(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:))$
- Hex ColorCSS hex colours: #rgb, #rgba, #rrggbb and #rrggbbaa.^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$
Or browse the full regex library.
More from the workbench
Fine print
Tools: on the house.
Engineer: by the hour.
The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.
I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.
