Best Way to Get All HTML Tags Regex? Need Help with a Reliable Pattern!

20 Replies, 1124 Views

Hey everyone!

So, I’ve been trying to *get all HTML tags regex* for a project, but man, it’s been a struggle. 😅 I’ve tried a few patterns, but they either miss some tags or grab too much junk.

Anyone got a reliable pattern to *get all HTML tags regex*? Like, something that works for most cases? I’m not looking for anything super fancy, just something that’ll catch `<div>`, `<p>`, `<a>`, etc., without breaking on weird edge cases.

Also, if you’ve got tips on handling nested tags or self-closing ones, that’d be awesome.

Thanks in advance! 🙏 (and sorry if this has been asked a million times lol)
Hey! Regex for HTML can be a pain, honestly. I’ve used `<(\w+)[^>]*>(.*?)<\/\1>` before, and it works decently for most tags. It’s not perfect, but it handles basic stuff like `<div>` and `<p>`. For self-closing tags, you might need to tweak it a bit.

If you’re dealing with messy HTML, I’d recommend using a proper HTML parser instead of regex. Python’s BeautifulSoup or JS’s DOMParser are way more reliable for this kind of thing.

Good luck!
Yo, regex is tricky for HTML, especially with nested tags. I’ve had some success with `/<[^>]+>/g`, but it’s not foolproof. It’ll grab everything between `<` and `>`, so it might catch stuff you don’t want.

For nested tags, regex just isn’t the best tool. Maybe check out tools like Cheerio (for JS) or lxml (for Python). They’re way better for parsing HTML.
Regex and HTML? Oof, that’s a tough combo. I’ve tried `/<(\w+)[^>]*>(.*?)<\/\1>/g` before, and it kinda works, but it struggles with self-closing tags like `<img />`.

If you’re serious about getting all HTML tags regex, maybe look into XPath or CSS selectors instead. They’re way more robust for this kind of thing.
Honestly, regex isn’t the best for HTML. It’s like using a hammer for a job that needs a screwdriver. I’d suggest using a library like BeautifulSoup or jsdom.

But if you’re set on regex, try `/<[^>]+>/g`. It’s simple and works for most cases, but don’t expect it to handle every edge case.
Regex for HTML is a rabbit hole, man. I’ve used `/<(\w+)[^>]*>(.*?)<\/\1>/g`, and it’s okay for basic tags, but it falls apart with nested stuff.

For self-closing tags, you might need to add something like `/<[^>]+\/>/g` to catch those. But honestly, just use an HTML parser. It’ll save you so much headache.
Hey! I’ve been down this road before. Regex is great for simple stuff, but HTML is just too messy. I’d recommend using a proper parser like BeautifulSoup or Cheerio.

If you’re dead set on regex, try `/<[^>]+>/g`. It’s not perfect, but it’ll catch most tags. Just be prepared for some edge cases.
Regex and HTML? That’s a risky combo. I’ve used `/<(\w+)[^>]*>(.*?)<\/\1>/g`, and it works for basic tags, but it’s not great for nested or self-closing tags.

For something more reliable, check out tools like jsdom or lxml. They’re way better for parsing HTML than regex.
Wow, thanks for all the replies, everyone! 🙌 I tried the `/<[^>]+>/g` pattern, and it’s working okay for now, but I see what you mean about edge cases. I’ll definitely check out BeautifulSoup and Cheerio—seems like they’re the way to go for anything more complex.

Quick follow-up: anyone have tips on handling malformed HTML with these tools? Like, what if the tags are broken or missing? Would love to hear your thoughts!

Thanks again, you all rock! 🚀
Regex for HTML is a nightmare, honestly. I’ve tried `/<[^>]+>/g`, and it’s okay for simple cases, but it’s not great for nested tags or self-closing ones.

If you’re serious about this, I’d recommend using a proper HTML parser. Regex just isn’t built for this kind of thing.



Users browsing this thread: 1 Guest(s)