0

I'm sorting through a CSV file splitting (using .match()) on commas outside of quotes, and what I have so far is working:

/"[^"]*"|[^,]+/g

The only problem is that there are several areas where the string might be something like:

...,xxx,,xxx,...

and what I want to have happen is that I get a an array from that of:

[...,xxx, ,xxx,...]

but instead I get

[...,xxx,xxx,...]

changing it to

 /"[^"]*"|[^,]*/g

doesn't work out so well. Any thoughts?

2
  • Provide some sample input values. Commented Nov 12, 2013 at 16:12
  • Take a look at this answer: stackoverflow.com/questions/6428053/… Commented Nov 12, 2013 at 16:21

4 Answers 4

2

You can use replace function like this:

var str = yourString.replace(/,,/g,', ,')


And after that you will get what You need with your regex, hope it helps. Cheers.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution seems to add some unwanted spaces in the string.
1

I tried to change your original regexp to also add empty elements if you have sequences of ,,. It doesn't add an empty element for "a",, but i assume that's not what you want

The regexp is:

/"[^"]*"|[^,]+|(?=,,)/g

Example usage:

var input = 'ACP,Something,,"Some long, sentence, with commas",other things';

var matches = input.match(/"[^"]*"|[^,]+|(?=,,)/g);

console.log(matches); // ["ACP", "Something", "", "\"Some long, sentence, with commas\"", "other things"]

DEMO: http://jsbin.com/uzaDeRe/1/edit

4 Comments

I'm using string.match(), not split, sorry about that. The regex works for what I'm doing except that if there is a ",," area in the string, it doesn't return anything instead of returning an empty item.
I think i understand what you want now. Check my edited post please.
Thanks! This works great. I'm not too familiar with regex yet, could you explain the (?=,,) part? I'm assuming it is just checking for ',,'.
It is called positive lookahead. Basically checks if the following characters are ,,, but does not consume them! So this matches any empty string that is followed by two commas. regular-expressions.info/lookaround.html
0

Silly question: Why are you using a regexp here?

console.log("aaa,xxx,,xxx,,yyy,zzz".split(","));

> ["aaa", "xxx", "", "xxx", "", "yyy", "zzz"]

1 Comment

Because the string i'm splitting looks something like "ACP,Something,,"Some long, sentence, with commas",other things" So it would break on commas inside of quotes.
0

Don't use replace. Instead split on a regexp values.split(/super awesome regexp/) However, you probably want better control. See this answer for a better solution.

1 Comment

this splits on every non comma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.