Skip to content Skip to sidebar Skip to footer

Split Comma Separated String By Comma

Need to split the string containing country names separated by comma(,) and also country name itself contains comma(,) too. var str = 'South Georgia and The South Sandwich Islands,

Solution 1:

Generally you don't want to use a character that could show up in valid countries as the deliminator, aka ,
However if we can assume that , will only show up without spaces around it when its used as a deliminator. Then we could use a regex to split the string:

var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland,Spain,Armenia,Mauritius,France,Sri Lanka,Aruba,Mayotte,French Guiana,Suriname,Australia,Mexico,French Polynesia,Svalbard and Jan Mayen,Austria,Micronesia, Federated States,French Southern Territories";
var res = str.split(/(?<=\w),(?=\w)/i);
console.log(res)

Regex explained:

  • (?<=\w) is a look behind for any "word" character.
  • (?=\w) is a look ahead for any "word" character.
  • , will match a comma char if and only if the look ahead and look behind succeeds.

Interactive example: https://regexr.com/42b3e

Edit:

Efter looking into an issue brought up by @BorisSokolov in the comments, its become clear that the regex implementation differs between the major javascript runtimes.

Runtimes tested:

  • V8(includes Node): Works as intended
  • SpiderMonkey: Throws SyntaxError: invalid regexp group. Turns out Mozilla haven't yet implemented the "possitive look behind" standard.
  • ChakraCore: Throws Script error. Same here, turns out microsoft haven't yet implemented the "positive look behind" standard either.

Looking at TC39 we can see that the "possitive look behind" is part of the ES2018 spec. So its expected to be implemented in all major browsers in the near future.


Solution 2:

If the format of the string is in the same format as above then first you need to split string with comma. if item in the result array begins with a space then merge that item with previous item in the result array. In country names with comma, there exists a space after comma while others not


Solution 3:

There is probably a way to do it by regular expression, but I would suggest the easy way. Looking at your input, you can see that those commas that separate the title of a country from it's name are followed by a space, whereas the listing comma isn't followed by one: var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland,Spain,Armenia,Mauritius,France,Sri Lanka,Aruba,Mayotte,French Guiana,Suriname,Australia,Mexico,French Polynesia,Svalbard and Jan Mayen,Austria,Micronesia, Federated States,French Southern Territories";

So in order to separate those two, I would suggest replacing ", " by a special character that would not occur in your input - say "$". Afterwards, you can split by ",". Then you can replace your special character back to ", ":

function getCountryList(str) {
  var strWithSpecialCharacterReplaced = str.replace(", ", "$");
  var countryList = strWithSpecialCharacterReplaced.split(",");
  return countryList.map(countryString => countryString.replace("$", ", "));
}

This is of course not the most performant solution. But it is one.


Solution 4:

Just use Regex!

var str = "South Georgia and The South Sandwich Islands,Congo, Democratic Republic,Mauritania,Finland,Spain,Armenia,Mauritius,France,Sri Lanka,Aruba,Mayotte,French Guiana,Suriname,Australia,Mexico,French Polynesia,Svalbard and Jan Mayen,Austria,Micronesia, Federated States,French Southern Territories";
var res = str.split(/(?<=\w),(?=\w)/i);
console.log(res)

In this example, I use (? = [A-zA-Z0-9]) [,] (? = [A-zA-Z0-9]) to get all the commas surrounded by letters and divide them (the " , "is not divided in this case and return is correct)! Tested and working!


Post a Comment for "Split Comma Separated String By Comma"