字符串拆分,避免使用“ \ n” JavaScript

时间:2019-03-01 10:56:45

标签: javascript string

var testString = 'Sr No\tMonth\tMill\tOrigin\tParty\tOrder Date\t"Order Comp.\nDt."\n57\tJan-19\tGINZA\tIND\tSBL\t28.01.2019\t10.02.2019';
var newLine = testString.split('\n');
Oputput of the newLine
[ 'Sr No\tMonth\tMill\tOrigin\tParty\tOrder Date\t"Order Comp.','Dt."','57\tJan-19\tGINZA\tIND\tSBL\t28.01.2019\t10.02.2019' ]

在这里,我想避免\n"Order Comp.\nDt."的字符串形式

[ 'Sr No\tMonth\tMill\tOrigin\tParty\tOrder Date\t"Order Comp.\nDt."','57\tJan-19\tGINZA\tIND\tSBL\t28.01.2019\t10.02.2019' ]

1 个答案:

答案 0 :(得分:1)

然后而不是分割字符串,为什么不匹配要保留的部分?

var testString = 'Sr No\tMonth\tMill\tOrigin\tParty\tOrder Date\t"Order Comp.\nDt."\n57\tJan-19\tGINZA\tIND\tSBL\t28.01.2019\t10.02.2019';

var out = testString.match(/(?:"[^"]*"|[^\n])+/g);

console.log(out);