-
-
Save pseudosavant/0187ea97d5fd87ce78bb to your computer and use it in GitHub Desktop.
This seems unnecessarily-specific. I think something more open will cover more edge cases:
let htmlTagRe = /<[a-z][\s\S]*>/i;
const matches = /<.+>/g.exec( html_data );
If you just want the tags themselves, this seems to work pretty well:
/<\/?[a-z][^>]*>/ig
@JamesNewton That looks a lot simpler. Is there a reason you do [a-z] when those would be included in [^>]?
I took it a step further and ended up with <\/?[^>]+>. I just removed the [a-z] and changed * to + since that was over matching to <> which isn't a tag.
@pseudosavant Only that I was going to extract the content of the tags letter. Your's is probably the shortest possible.
Is there a reason you do
[a-z]when those would be included in[^>]?
Ensuring there is a letter right at the start of the opening or closing tag is good; it keeps you from erroneously matching strings like "If 3 < 4 > 2 etc"
You're regular expression doesn't work if it's an
<a>with anhrefwith query params... you need to add the?to you're expression -