如何保留引号,如何在逗号上拆分文字而不是双引号? [英] How can I split text on commas not within double quotes, while keeping the quotes?

查看:103
本文介绍了如何保留引号,如何在逗号上拆分文字而不是双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在javacript中拆分一个字符串,看起来像这样:

So I'm trying to split a string in javacript, something that looks like this:

"foo","super,foo"

现在,如果我使用 .split(, )它会将字符串转换为包含 [0]foo[1]super [2] foo
的数组但是,我只想拆分引号之间的逗号,如果我使用 .split(','),它将变为 [0]foo [1] super,foo

Now, if I use .split(",") it will turn the string into an array containing [0]"foo" [1]"super [2]foo" however, I only want to split a comma that is between quotes, and if I use .split('","'), it will turn into [0]"foo [1]super,foo"

有没有办法可以拆分表达分隔符的元素,但是有它保留某些分隔符而不必编写代码将值连接回字符串?

Is there a way I can split an element expressing delimiters, but have it keep certain delimiters WITHOUT having to write code to concatenate a value back onto the string?

编辑:

我希望得到 [0]foo ,[1]super,foo作为我的结果。基本上,我需要编辑某些数据的方式,我需要[0]中的内容永远不会改变,但[1]的内容将根据它包含的内容而改变。它将连接回来看起来像foo,我已经改变了或者它确实会保持不变如果[1]的内容不是需要的东西更改

I'm looking to get [0]"foo",[1]"super,foo" as my result. Essentially, the way I need to edit certain data, I need what is in [0] to never get changed, but the contents of [1] will get changed depending on what it contains. It will get concatenated back to look like "foo", "I WAS CHANGED" or it will indeed stay the same if the contents of [1] where not something that required a change

推荐答案

试试这个:

'"foo","super,foo"'.replace('","', '"",""').split('","');

这篇关于如何保留引号,如何在逗号上拆分文字而不是双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆