1

重複の可能性:
CSVデータを解析するためのJavascriptコード

私はこの文字列を持っています:

"display, Name" <test@test.com>, display" Name <test@test.com>, test@test.com

この文字列を配列に分割したい

array[0] = "\"display, Name\" <test@test.com>"
array[1] = "display\" Name <test@test.com>"
array[2] = "test@test.com"

これが私のコードです:

var comma = inQuotes = false;
for(var i=0;i<str.length;i++) {
            if (str[i] == '"') inQuotes = !inQuotes;
            comma = (str[i] == "," && !inQuotes)  ? true : false;
            item += (!comma) ? str[i] : "";
            if(comma || i == str.length-1) {  
                items.push(item);
                item = "";
            }    
        } 

私の問題は、文字列にクローザーがない二重引用符が1つある場合です

私は助けに感謝します...

4

1 に答える 1

1

私はこれを試してみましたが、上記の文字列jsfiddleで機能しています

var a = '"display, Name" <test@test.com>, display" Name <test@test.com>, test@test.com';
var aa = a.match(/"(.*?)"(.*?),|(.*?),|(.*?)$/g); 
aa.pop();
aa // [""display, Name" <test@test.com>,", " display" Name <test@test.com>,", " test@test.com"]
于 2012-11-09T21:50:28.777 に答える