RegexRef
🏷️

Regex for Extract HTML Tags

Matches HTML tags and their attributes.

The Pattern

/<\/?([a-zA-Z0-9]+)(?:\s+[^>]+)?>/g
Test in Visualizer ↗

✅ Matches

  • <div class="test">
  • </div>
  • <img>

❌ Non-Matches

  • 1 < 2
  • Not a tag

Implement in your Code

javascript
const regex = /<\/?([a-zA-Z0-9]+)(?:\s+[^>]+)?>/g;
const str = '<div>test</div>';
const tags = str.match(regex);
python
import re
regex = re.compile(r'<\/?([a-zA-Z0-9]+)(?:\s+[^>]+)?>')
tags = regex.findall('<div>test</div>')
go
import "regexp"
re := regexp.MustCompile(`<\/?([a-zA-Z0-9]+)(?:\s+[^>]+)?>`)
tags := re.FindAllString(`<div>test</div>`, -1)
rust
use regex::Regex;
let re = Regex::new(r"<\/?([a-zA-Z0-9]+)(?:\s+[^>]+)?>").unwrap();
let tags: Vec<_> = re.find_iter("<div>test</div>").map(|m| m.as_str()).collect();