0

I need to split a string in javascript (no jquery). This is basically received fron csv. so, i have a string,

string = '"abc, xyz, mno","lmn, pqr, bcd, jkl"';

Note: Number of commas in quoted parts is not fixed for example, "abc, xyz, mno" can also be "abc, xyz, mno, klm, ..." or "lmn, pqr, bcd, jkl" could have been just "lmn"; i.e no comma at all.

I want to split it as

str1 = 'abc, xyz, mno';
str2 = 'lmn, pqr, bcd, jkl';

I can't do this with string.split(",");, due to undefined number of commas.

Thanks for any help in advance.

Barmar
  • 741,623
  • 53
  • 500
  • 612
N41
  • 11
  • 5
  • Why can't you use `string.split('","');` and then remove the excess `"` with `string.replace("\"", "");` ? – Zak Aug 28 '18 at 19:20
  • string.split(",") tends to split the string on all the commas. so when i string.split() on '"abc, xyz, mno","lmn, pqr, bcd, jkl"' it will return 7 parts not 2 :( – N41 Aug 28 '18 at 19:23
  • I corrected .. I meant to split on the quotes AND commas – Zak Aug 28 '18 at 19:23
  • Why not split on ," ? – Jeroen Heier Aug 28 '18 at 19:23
  • 1
    Does `JSON.parse("[" + '"abc, xyz, mno","lmn, pqr, bcd, jkl"' + "]")` work? – Sebastian Simon Aug 28 '18 at 19:25
  • Is this coming from a CSV file? There are Javascript CSV libraries that can parse it. – Barmar Aug 28 '18 at 19:26
  • I can't do split on ," too, since in csv " is optional; so if i take case 2 of note i gave, '"abc, xyz, mno",lmn', here there is no quote to split on. it could also be just 'abc,lmn' :( – N41 Aug 28 '18 at 19:26
  • @Xufox, I'll try that – N41 Aug 28 '18 at 19:28
  • @Barmar do you know any? any links will be appreciated – N41 Aug 28 '18 at 19:28
  • @N41 See the duplicate links I just provided – Barmar Aug 28 '18 at 19:30
  • @N41 If the `"` quotes are optional, then `JSON.parse` also won’t work. You’d need to use a CSV parser. (Time for a TC39 proposal on a built-in CSV parser?) – Sebastian Simon Aug 28 '18 at 19:31
  • Thanks @Barmar for the link, I'll check for possible solutions there, also, I'll go through below answers and update. :) – N41 Aug 28 '18 at 20:00
  • @Xufox, probably :P – N41 Aug 28 '18 at 20:03
  • Thanks @Barmar for the link. I modified the solution provided by _@niry_, works great ;) – N41 Aug 28 '18 at 20:34
  • Modified ans for ref :: function c2A(text){let p='',row=[''],ret=[row],i=0,r=0,s=!0,l;for(l of text){if('"'===l){if(s&&l===p)row[i]+=l;s=!s;}else if(','===l&&s)l=row[++i]='';else if('\n'===l&&s){if('\r'===p)row[i]=row[i].slice(0,-1);row=ret[++r]=[l=''];i=0;}else row[i]+=l;p=l;}return ret;};function t1(str1){let test=str1;var tab1=c2A(test);var str=[''];for(i=0;i<2;i++)str[i]=tab1[0][i];document.write("InputString:"+str1+"");document.write("str1="+str[0]+''+"str2="+str[1]+'
    ');}t1('"abc,xyz,mno","lmn,pqr,bcd,jkl"');t1('"abc,xyz,mno",lmn');t1('mno,lmn');
    – N41 Aug 28 '18 at 20:46

2 Answers2

1

You can specify multiple characters in the String#split method :

str.split('","')

Demo:

let str = '"abc, xyz, mno","lmn, pqr, bcd, jkl"';

let result = str.split('","')
console.log(result);

If you want to get rid of the quotes, juste add a

.map(s => s.replace('"',''))

Demo:

let str = '"abc, xyz, mno","lmn, pqr, bcd, jkl"';

let result = str.split('","').map(s => s.replace('"',''));
console.log(result);
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • Thanks for your answer @Zenoo, It does work if the string has quotes. Since quotes are optional, I cannot use this solution. However, I have found the solution on duplicate link provided. :) – N41 Aug 28 '18 at 20:50
0

This is probably best done with a regular expression. But, since they aren't my strongest area, here's how it could be done with a combination of String.split(), Array.map(), and String.replace():

let input = '"abc, xyz, mno","lmn, pqr, bcd, jkl"';

// Split where "," is found
// Create a new array that is a copy of the original
// but with \" removed from the elements
let output = input.split('","').map(s => s.replace('\"',''));

console.log(output);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Thanks for your answer @Scott, It does work if the string has quotes. Since quotes are optional, I cannot use this solution. However, I have found the solution on duplicate link provided. :) – N41 Aug 28 '18 at 20:51