-1

I'm trying to convert a comma or semicolon separated string into an array, but handling before conversion several omission from users (double commas, space after comma or semicolon). In the example:

myStr = "item1, item2,,, this is item3; item4 , "

I want to get:

myArray = ["item1", "item2", "this is item3", "item4"]

My actual regex is:

myArray = myStr.split(/[(,\s);,]+/);

But, although I'm adding parenthesis to the combination comma-space (,\s) the regex are catching the spaces inside the third item. Any advice what could be wrong with regex?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Ruben
  • 816
  • 1
  • 8
  • 21
  • Possible duplicate of [Convert comma separated string to array](http://stackoverflow.com/questions/2858121/convert-comma-separated-string-to-array) –  Jun 01 '16 at 01:25
  • Not a duplicate; this question also has empty item elimination and isn't just commas. – Jacob Jun 01 '16 at 01:26
  • @Mike, is not the same. I want to trim spaces, not using them as separators. – Ruben Jun 01 '16 at 01:59
  • @Reuben, I see. I updated my answer to fit the bill for you – mike510a Jun 01 '16 at 02:11

4 Answers4

2

Try this (edited):

var myStr = "item1, item2,,, this is item3; item4 , ";
var myArray = myStr.split(/\s*[,;]+\s*/).filter(Boolean);
alert(myArray);
Ruben
  • 816
  • 1
  • 8
  • 21
Jaumzera
  • 2,305
  • 1
  • 30
  • 44
1

[] only contains single characters or character classes and only matches one character at a time. Therefore, [(,\s);,] really means any of these characters are matched: (),; or whitespace.

There are easier approaches than regex. Try this:

myArray = myStr
  .split(/[;,]/)                 
  .map(function (str) { 
    return str.replace(/(^\s+)|(\s+$)/g, ''); // Trim whitespace
  }) 
  .filter(Boolean);              // Filter away the empty strings
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Wouldn't this return `["item1", "item2", "this", "is", "item3", "item4", ""]`? – choz Jun 01 '16 at 01:21
  • @choz, The `.filter(Boolean)` removes any empty entries. – Jacob Jun 01 '16 at 01:25
  • Thanks @Jacob. Your code works great. Anyway I give the Ok to the answer using regex. I used your `filter(Boolean)` trick to delete the empty item in array which regex can't delete. – Ruben Jun 01 '16 at 02:31
1

I think you need to ensure you always match at least one non-white-space character. Try this:

myArray = myStr.split(/[\s]*[,;][,;\s]*/);
Alastair Brown
  • 1,598
  • 8
  • 12
1
var myArray = myStr.split(/(?: , | ; | ,| ;|, |; |;|,)+/);  

 Then do a quick trim() and verify if the item has content other than whitespace

Would effectively split a string using , or ; as the separator

var myStr = "item1, item2,,, this is item3; item4 , ";
var myArray = myStr.split(/(?: , | ; | ,| ;|, |; |;|,)+/); 
var newArray = [];
for (var i=0; i<myArray.length; i++) {
 var checkArray = myArray[i].trim();
 if (checkArray.length > 0) { newArray.push(myArray[i]); }
}
myArray = newArray;

console.log(myArray);

ANSWER 2

The best answer I could come up with was one I made by combining @Jacob's answer and my answer above.

This will effectively Accomplish the job in the fastest way possible

var myStr = "item1, item2,,, this is item3; item4 , ";
var myArray = myStr.split(/(?:,|;)+/).map(function (str) {
  return str.replace(/(^\s+)|(\s+$)/g, '');
 }).filter(Boolean);
console.log(myArray);
mike510a
  • 2,102
  • 1
  • 11
  • 27