我有一个这样的字符串
var str = "n,m,'klj,klp',ml,io"; // not the quotes arround klj,klp
我使用 javascripts .split()
但它返回如下
n
m
klj
klp
ml
io
但我需要如下,不知道
n
m
klj,klp
ml
io
最佳答案
丑陋,简单:
"n,m,'klj,klp',ml,io,'test,test,a,b','test',test".
match( /'[^']+'|[^,]+/g ).
map( function( x ) { return x.replace( /^'|'$/g, '' ) } );
结果:
["n", "m", "klj,klp", "ml", "io", "test,test,a,b", "test", "test"]
如果此示例来自 CSV 文件,您必须留意更多问题。
关于javascript - 使用 javascript 在字符串内用逗号分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27076697/