admin管理员组

文章数量:1435859

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 pressor</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);
        }           
    }   

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 pressor</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);
        }           
    }   
Share Improve this question edited Aug 23, 2014 at 21:54 이승현 asked Aug 23, 2014 at 13:37 이승현이승현 1113 silver badges9 bronze badges 1
  • Don't try to parse HTML with regex; use an XML parser. – royhowie Commented Aug 23, 2014 at 13:44
Add a ment  | 

2 Answers 2

Reset to default 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 piling either, you need to mix single and double quotes:

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

All together looks like

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

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

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

And it seems to work fine on my end.

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","pressor","<div class="tab1"></div>"]

I want like this

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

本文标签: javascripthow to use split() function with html tag and whitespaceStack Overflow