Skip to content

Instantly share code, notes, and snippets.

@pseudosavant
Last active August 31, 2023 12:42
Show Gist options
  • Save pseudosavant/0187ea97d5fd87ce78bb to your computer and use it in GitHub Desktop.
Save pseudosavant/0187ea97d5fd87ce78bb to your computer and use it in GitHub Desktop.
Javascript Regular Expression for finding HTML tags in a string
const htmlTagRe = /<\/?[^>]+>/gi;
const html = '<span class="error" style="background: none; font-size: 1em;" data-attr=testing123><span disabled class="code">Status text blah blah blah</span> <span class="text">HTTP 500 : Internal Server Error</span></span>';
const plainText = html.replace(htmlTagRe, '');
console.log(plainText); // status text blah blah blah HTTP 500 : Internal Server Error
@matthewdowns-eb
Copy link

This seems unnecessarily-specific. I think something more open will cover more edge cases:

let htmlTagRe = /<[a-z][\s\S]*>/i;

@yukal
Copy link

yukal commented Jul 11, 2019

const matches = /<.+>/g.exec( html_data );

@JamesNewton
Copy link

If you just want the tags themselves, this seems to work pretty well:
/<\/?[a-z][^>]*>/ig

@pseudosavant
Copy link
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.

@JamesNewton
Copy link

@pseudosavant Only that I was going to extract the content of the tags letter. Your's is probably the shortest possible.

@otherjoel
Copy link

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