0

I am trying to separate HTML-tagName from HTML-Code. My HTML-Code is as follows:

<div id="MyDiv" style="left:100px; top:10px;" > some text in div
<strong>
<em> Some text for em
<p><b>b,  <span id="MySpan">span1,

After calling of REg-Ex I expect all valid HTML-Tagname from string. E.g. from above HTML-Code it should result something as follows: div, strong, em, p, b, span. Here is my approach:

sTagName = sTagName.replace(/< *(.*) *>/, '$1');
alert(sTagName);

The above RegEx-Statement delivers for < div > results: div This is ok. But it delivers for < div stlye="..." > results: div stlye="..." This is wrong. I expect for it only a div. In other words after space in HTML-Tagname the string should be deleted, so that I get div-Tagname. For performance reasons I won't call the RegEx-Statement twice. Thanks in advance.

user3815508
  • 369
  • 4
  • 20
  • 6
    Related: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – James Donnelly Oct 08 '14 at 15:40
  • Can't figure out what you're trying to do. Are you trying to match tags without attr/val's or strip attr/vals ? –  Oct 08 '14 at 15:46

1 Answers1

1

Via the dom;

var el = $("<div/>").append(html_string).find("*").each(function() {
    alert($(this).prop("tagName"));
});
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Yes, perfectly! Do you know, whether it is faster (performance) than using of RegExp e.g. something as follows: `sTagName = sTagName.replace(/<\s*(\w+)[^/>]*>/, '$1');` – user3815508 Oct 08 '14 at 16:57
  • I was hasty. The DOM delivers some tagName twice and disordered. I can't use it. I'll try better with RegEx. Here are my approaches:`sTagName = sTagName.replace(/(<.*?>)(.*?)]*>.*? – user3815508 Oct 09 '14 at 06:32