根据这个:C# String splitting - breaking string up at second comma
我如何在javascript中执行相同操作?
答案 0 :(得分:2)
var mystring = "test1, 1, anotherstring, 5, yetanother, 400";
var subString = mystring.slice(0,mystring.indexOf(',',mystring.indexOf(',')+1));
或更漂亮:
var index = mystring.indexOf(',',mystring.indexOf(',')+1);
var substring = mystring.slice(0,index);
答案 1 :(得分:1)
使用此...
var str="test1, 1, anotherstring, 5, yetanother, 400";
var n=str.match("([^,]*,[^,]*)(?:, |$)");
console.log(n);
答案 2 :(得分:1)
您也可以在没有正则表达式的情况下执行此操作:
var splitAtSecondComma = function (str) {
var arr2 = str.split(",");
var arr1 = arr2.splice(0,2);
return [arr1.join(","), arr2.join(",")];
};
答案 3 :(得分:1)
试试这个
function splitSecondComma(str) {
var arr = str.split(","),
l = arr.length,
arr2 = [];
for(i= 0; i < l; i= i+2) {
arr2.push(arr[i] + " " + arr[i+1]);
}
return arr2
}