1
var str = '"testStr, 10.0 pl",NA,4.6';
var rawLine = str.split(',');
console.log(rawLine[0]);
console.log(rawLine[1]);
console.log(rawLine[2]);

It is giving result as:

""testStr"
"10.0 pl""
"NA"

Where I am looking for below result:

"testStr, 10.0 pl"
"NA"
"4.6"
Bharat
  • 407
  • 2
  • 7
  • 16
  • 1
    Am I correct to assume that you want to ignore commas based on whether they are inside quoted strings? – Touffy Apr 05 '15 at 16:33
  • 1
    yeah, it's a duplicate of that question, but unfortunately the answer there isn't quite good as it ignores the possibility of escaped quotes (`'\"'`) – Touffy Apr 05 '15 at 17:08
  • 1
    Consider using _JSON_ instead of your current notation for your _String_ – Paul S. Apr 05 '15 at 18:24

3 Answers3

2

If you would prefer not to parse as another answer suggests, I would split on quoted expressions and commas:

function split_string_on_commas_outside_quotes(str) {

  return str . 

    // Replace escaped quotes; will put back later.
    replace(/\\"/g, "__QUOTE__") .

    // Split on quoted strings and commas, but keep in results.
    // Use feature of split where groups are retained in result.
    split(/(".*?"|,)/) .

    // Remove empty strings and commas from result.
    filter(function(piece) { return piece && piece !== ','; }) .

    // Remove quotes at beginning and end of quoted pieces as you want.
    map(function(piece) { return piece.replace(/^"|"$/g, '') }) .

    // Restore escaped quotes.
    map(function(piece) { return piece.replace(/__QUOTE__/g, "\\\""); })
  ;

}

>> var str = '"testS\"tr, 10.0 pl",NA,4.6'
>> split_string_on_commas_outside_quotes(str)
<< ["testS\"tr, 10.0 pl", "NA", "4.6"]
0

Parsing delimiters such as quotes (or parens, brackets etc but especially quotes for a couple of reasons) is best done with a CFG parser, not regular expressions. But it's quite easy, and done in O(n) time which is the same as regular expressions, and better than the irregular expressions you might end up using for this sort of thing (REs are native though).

function parseStrings(str){
  var parse=[], inString=false, escape=0, end=0

  for(var c=0; c<str.length; c++) switch(str[c]){
    case '\\': escape^=1; break
    case ',': if(!inString){
        parse.push(str.slice(end, c))
        end=c+1
      }
      escape=0
      break
    case '"': if(!escape) inString=!inString
    default: escape=0 // fallthrough from previous case
  }
  if(inString) throw SyntaxError('expected matching " at the end of the string')
  if(end<c) parse.push(str.slice(end, c))
  return parse
}

That can be extended to parse single quoted strings and other delimiters too (you'd have to build a stack for non-quote delimiters). I posted a modified version that handles both single and double quotes in Regex to pick commas outside of quotes

Community
  • 1
  • 1
Touffy
  • 6,309
  • 22
  • 28
-2

You should use regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Jaakko
  • 584
  • 6
  • 13