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?

Evan Ward
  • 1,371
  • 2
  • 11
  • 23

4 Answers4

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.

Kamil Wozniak
  • 430
  • 3
  • 7
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

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • 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. – Evan Ward Nov 12 '13 at 16:28
  • I think i understand what you want now. Check my edited post please. – Tibos Nov 12 '13 at 16:39
  • 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 ',,'. – Evan Ward Nov 12 '13 at 18:58
  • 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. http://www.regular-expressions.info/lookaround.html – Tibos Nov 12 '13 at 19:00
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.

Community
  • 1
  • 1
Sukima
  • 9,965
  • 3
  • 46
  • 60
0

Silly question: Why are you using a regexp here?

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

> ["aaa", "xxx", "", "xxx", "", "yyy", "zzz"]
eightyfive
  • 4,601
  • 3
  • 35
  • 44
  • 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. – Evan Ward Nov 12 '13 at 16:25