javascript - 使用避免 "\n"JavaScript 进行字符串分割

标签 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' ]

在这里,当“Order Comp.\nDt.”采用字符串形式时,我想避免使用\n

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

最佳答案

那么为什么不匹配您想要保留的部分,而不是分割字符串呢?

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);

关于javascript - 使用避免 "\n"JavaScript 进行字符串分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54943212/

相关文章:

javascript - 未处理的 promise 拒绝警告 : SyntaxError: Unexpected token o in JSON at position 1

javascript - 尝试在某个类上执行每个功能

javascript - 未捕获的类型错误 : Object prototype may only be an Object or null on ember-1. 0.0-pre.2

c - 我将如何使用 strchr 替换此示例中的字符?

java - 将字符串转换为长整型,无内置库

c# - 使用文化信息将 unix 时间转换为特定的日期时间格式

javascript - 如何在调用时从 spy.on(object, 'funcName') 返回虚假数据?

javascript - 在 jQuery slider 上方显示编号间隔

python - 如何在 Python 中将字符串转换为 "command"?

c++ - 索引到字符串时输出不正确