Last active
August 31, 2023 12:42
-
-
Save pseudosavant/0187ea97d5fd87ce78bb to your computer and use it in GitHub Desktop.
Javascript Regular Expression for finding HTML tags in a string
If you just want the tags themselves, this seems to work pretty well:
/<\/?[a-z][^>]*>/ig
Author
@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"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const matches = /<.+>/g.exec( html_data );