-1

Below is some string, which should be serve as my HTML-Code. I am trying from below string or HTML-Code separate the HTML-Tagname. After processing on the string the result should be something like as follows: =div=div=strong=em=p=b=p=p=h4=h1=span=. Here is my HTML-Code in the variable "sTagName":

var sTagName = 'abc<div style="left:100px;" > some <div>MyText, <strong> hgz uz <em> Some text for flrdm <p><b>b,  <p> <p><h4><h1><span id="MySpan">any text, ';

Here is my solution:

// Remove all attributes, e.g. <div style="left:100px;" > will be converted to <div>
sTagName = sTagName.replace(/<([a-zA-Z0-9]+).*?>.*?/g, '<$1>' );
// I add the "<>" at end of HTML-Code in order to remove the last useless string, I mean "Any text, "
sTagName = sTagName + "<>";
sTagName = sTagName.replace(/.*?<(.*?)>.*?/g,'=$1');

alert(sTagName);

The function alert(sTagName) delivers the expected result. But I want improve my method referring to performance. E.g. I would like to build from two RegEx one RegEx, or something like that. Any idea? Thanks in advance.

user3815508
  • 369
  • 4
  • 20
  • 2
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – emerson.marini Oct 09 '14 at 14:02
  • Wrong! I am not looking for a solution. But I want to improve my solution. I already read this link and with means of it written my current solution. As I written, I am trying to improve my methode. – user3815508 Oct 09 '14 at 14:40
  • Code improvements should be posted on [Code Review Stack Exchange](http://codereview.stackexchange.com/) – emerson.marini Oct 09 '14 at 14:42
  • Oh my goodness! I hate to get minus points. Therefore, I have long ago declared.:-( In addition, the link there contains among other Perl-RegEx-Code and is no longer clear. – user3815508 Oct 09 '14 at 14:54

4 Answers4

1

enter image description here

Use DOM:

var sTagName = 'abc<div style="left:100px;" > some <div>MyText, <strong> hgz uz <em> Some text for flrdm <p><b>b,  <p> <p><h4><h1><span id="MySpan">any text, ';


tags = $("<div>").html(sTagName).find("*").map(function() {
  return this.nodeName;
}).toArray();

document.write(tags);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
georg
  • 211,518
  • 52
  • 313
  • 390
  • Yes, you're right. I've tried before. But unfortunately DOM under certain circumstances gives me double tagNamea and the order of tagNames is not as I expected. So I try with RegEx. – user3815508 Oct 09 '14 at 14:29
1

You can do that:

var sTagName = 'abc<div style="left:100px;" > some <div>MyText, <strong> hgz uz <em> Some text for flrdm <p><b>b,  <p> <p><h4><h1><span id="MySpan">any text, ';

var arr = new Array;
var result;
var re = /<(\w+)/g;

while ((m = re.exec(sTagName)) !==null) {
    arr.push(m[1]);
}

result = '=' + arr.join('=') + '=';

console.log(result);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

Try

sTagName = $.map(sTagName.split(/[^<\w+]/), function(v, k) {
  return /</.test(v) ? v.replace(/[a-z]+<|</g, "=") : null
}).join("").concat("=");

var sTagName = 'abc<div style="left:100px;" > some <div>MyText, <strong> hgz uz <em> Some text for flrdm <p><b>b,  <p> <p><h4><h1><span id="MySpan">any text, ';

sTagName = $.map(sTagName.split(/[^<\w+]/), function(v, k) {
  return /</.test(v) ? v.replace(/[a-z]+<|</g, "=") : null
}).join("").concat("=");

$("body").text(sTagName)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
guest271314
  • 1
  • 15
  • 104
  • 177
0
<(\w+)\s*[^>]*>|.(?=([^><]*<[^>]*>)*[^<>]*$)

Try this.Replace by $1.Later on append = to each result.

See demo.

http://regex101.com/r/qZ0uP0/2

vks
  • 67,027
  • 10
  • 91
  • 124