I have a single string, which is itself a ma delimited list of quoted strings - which can have mas within them.
Example:
var str = '"long one","short","oh, look","wow.", ""';
I need to split this into an array:
['long one', 'short', 'oh, look', 'wow.', '']
// will take this if it is easier
['"long one"', '"short"', '"oh, look"', '"wow."', '""']
I tried splitting by ,
but it throws off entries with a ma in them. I also tried splitting by \",
, but it creates lob-sided string:
['"long one', '"short', '"oh, look', '"wow.', '"']
I tried splitting by \",\"
but causes the same issue but just for the first and last entry:
['"long one', 'short', 'oh, look', 'wow.', '"']
I also tried the regex expression found in this answer, but it adds an empty entry at the start and end of the array:
['', '"long one"', '"short"', '"oh, look"', '"wow."', '""', '']
Any suggestions?
Thanks.
I have a single string, which is itself a ma delimited list of quoted strings - which can have mas within them.
Example:
var str = '"long one","short","oh, look","wow.", ""';
I need to split this into an array:
['long one', 'short', 'oh, look', 'wow.', '']
// will take this if it is easier
['"long one"', '"short"', '"oh, look"', '"wow."', '""']
I tried splitting by ,
but it throws off entries with a ma in them. I also tried splitting by \",
, but it creates lob-sided string:
['"long one', '"short', '"oh, look', '"wow.', '"']
I tried splitting by \",\"
but causes the same issue but just for the first and last entry:
['"long one', 'short', 'oh, look', 'wow.', '"']
I also tried the regex expression found in this answer, but it adds an empty entry at the start and end of the array:
['', '"long one"', '"short"', '"oh, look"', '"wow."', '""', '']
Any suggestions?
Thanks.
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Aug 29, 2016 at 15:15 MacMac 1,1736 gold badges21 silver badges47 bronze badges 5- 1 Why not use the regex you found then remove the first and last elements? – Mike Cluck Commented Aug 29, 2016 at 15:16
-
4
Match
"(.*?)"
– Tushar Commented Aug 29, 2016 at 15:17 - Do these e from a csv file? – Tamas Hegedus Commented Aug 29, 2016 at 15:19
- yes, they do e from a CSV file. @MikeC, you're right, I could do that. I suppose I just didn't want to risk lobbing of some data - on the off chance the extra entries were not attached – Mac Commented Aug 29, 2016 at 15:40
-
1
Since quotes don't appear in quoted strings, and your data is delimited by quotes, it's really as simple as finding all
"([^"]*)"
. This is the simplest solution. If you later add caveats to this simple description, a fuller regex is necessary. – user557597 Commented Aug 29, 2016 at 15:50
3 Answers
Reset to default 7You could treat it as part of a JSON string and append the necessary parts for parsing as array.
var string ='"long one","short","oh, look","wow.", ""',
array = JSON.parse('[' + string + ']');
console.log(array);
You can .split()
ma ,
characters if ma is followed by double-quote "
character using RegExp
/,(?=")/
str.split(/,(?=")/)
var s = '"long one","short","oh, look","wow.", ""';
var answer = s.split(/("*,.?")/gi).filter(function (a) {
return a.replace(/"/g, '').length > 2
}).map(function (a) {
return a.replace(/"/g, '')
});
console.log(s);
console.log(answer);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745416543a4626775.html
评论列表(0条)