1

I use split(" ") for whitespace and use split(/(<[^>]*>)/) for html tag in string.

but i have to use together. i want to divide a string by whitespace and html tag and put it into an array. i don't want to lose anything. string, html tag both.

whitespace is \s and html tag is split(/(<[^>]*>)/) and i used like this new RegExp("\s+(<[^>]*>)", "g");

but it doesn't work.

var htmlTagRegex = new RegExp("\s+(<[^>]*>)", "g");     

    var str = ""<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>";
    var myArray = str.split(htmlTagRegex);

    if(myArray != null){
        for ( i = 0; i < myArray.length; i++ ) { 
            var result = "myArray[" + i + "] = " + myArray[i]+"<br />";
            $(".tt-sns-clear").append(result);
        }           
    }   
이승현
  • 81
  • 2
  • 9

2 Answers2

2

Not 100% sure what you are trying to do but it looks like the space needs to be optional like \s* instead of \s+

Something like : \s*(<[^>]*>)

And since you are not concatenating a string to your Regex better is:

var htmlTagRegex =/\s*(<[^>]*>)/g

The input string is not using double quotes correctly and it is not compiling either, you need to mix single and double quotes:

 '<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>';

All together looks like

    var htmlTagRegex =/\s*(<[^>]*>)/g     

    var str = '<div class="tab0">CSS code formatter</div><div class="tab2">CSS code compressor</div>';
    var myArray = str.split(htmlTagRegex);

//outputs ["", "<div class="tab0">", "CSS code formatter", "</div>", "", "<div class="tab2">", "CSS code compressor", "</div>", ""]

And it seems to work fine on my end.

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
0

string is from mysql. And that is inclueded html tag.

And I used like this: var htmlTagRegex =/\s|(<[^>]*>)/g;

but result is not what i want.

["","CSS","code","formatter","<div class="tab0"></div>","","CSS","code","compressor","<div class="tab1"></div>"]

I want like this

["<div class="tab0">","CSS","code","formatter","","<div class="tab1">","CSS","code","compressor","</div>"]
이승현
  • 81
  • 2
  • 9