javascript 拆分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3548307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript split
提问by vishnu
I can use JavaScript's split to put a comma-separated list of items in an array:
我可以使用 JavaScript 的 split 将逗号分隔的项目列表放入数组中:
var mystring = "a,b,c,d,e";
var myarray = mystring.split(",");
What I have in mind is a little more complicated. I have this comma separated string:
我的想法有点复杂。我有这个逗号分隔的字符串:
"mystring_109_all,mystring_110_mine,mystring_125_all"
how do i split this string in to an array
我如何将此字符串拆分为数组
回答by Andy E
You can provide a regular expressionfor split(), so to split on a comma or an underscore, use the following:
您可以为split()提供正则表达式,因此要在逗号或下划线上拆分,请使用以下内容:
var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray = mystring.split(/[,_]/);
If you're after something more dynamic, you might want to try something like "Search and don't replace", a method of using the replace()function to parse a complex string. For example,
如果您想要更动态的东西,您可能想尝试类似"Search and don't replace"的方法,这是一种使用replace()函数来解析复杂字符串的方法。例如,
mystring.replace(/(?:^|,)([^_]+)_([^_]+)_([^_]+)(?:,|$)/g,
function (var myCommaStrings = myString.split(',');
var myUnderscoreStrings = [];
for (var i=0;i<myCommaStrings.length;i++)
myUnderscoreStrings[myUnderscoreStrings.length] = myCommaStrings[i].split('_');
, first, second, third) {
// In this closure, `first` would be "mystring",
// `second` would be the following number,
// `third` would be "all" or "mine"
});
回答by mplungjan
Same, but loop
相同,但循环
var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray = mystring.split(",");
for (var i = 0; i < myarray.length; i++) {
myarray[i] = myarray[i].split("_");
}
回答by Jamie Wong
Throwing a wild guess, given your spec isn't complete:
鉴于您的规范不完整,大胆猜测:
var split1 = theString.split(',');
var split2 = [];
for (var i = 0; i < split1.length; ++i)
split2.push(split1[i].split('_'));
回答by Pointy
If you want to split on commas and then on underscores, you'd have to iterate over the list:
如果你想在逗号上分割,然后在下划线上分割,你必须遍历列表:
var mystring = "mystring_109_all,mystring_110_mine,mystring_125_all";
var myarray = mystring.split(",");
If you want to split on commas orunderscores, you can split with a regex, but that's sometimes buggy. Here's a page to read up on the issues: http://blog.stevenlevithan.com/archives/cross-browser-split
如果你想用逗号或下划线分割,你可以用正则表达式分割,但这有时会有问题。这是一个可以阅读这些问题的页面:http: //blog.stevenlevithan.com/archives/cross-browser-split
回答by donohoe
Umm, the same way you would your original example:
嗯,就像你原来的例子一样:
<!DOCTYPE html>
<html>
<body>
<p>Question is var str = "a,b,c,d,e,f,g"; </p>
<button onclick="myFunction()">Split Button</button>
<p id="demo"></p>
<p id="demo1"></p>
<script>
var text = "";
function myFunction() {
var str = "a,b,c,d,e,f";
var arr = str.split(",");
for(var i = 0; i<arr.length; ++i){
text += arr[i] +" ";
document.getElementById("demo").innerHTML = text;
}
document.getElementById("demo1").innerHTML = arr[0];
}
</script>
</body>
</html>
**Answer**
a b c d e f g
Arr[0]=a

