admin管理员组文章数量:1026989
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
2 Answers
Reset to default 2Not 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>"]
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
2 Answers
Reset to default 2Not 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
版权声明:本文标题:javascript - how to use split() function with html tag and whitespace - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657472a2161683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论