Combining multiple delimiters (split) in Javascript

I have a use case where the user could paste something in a text area and I'd like to split them based on the following use cases:

  1. Comma-space
  2. Comma-newline
  3. Newline


How can I make sure I can catch comma space, comma new line, newline from the string? Any regex would be helpful.


I could do

"1, 2, 3".split(', ') // returns [1,2,3]
"1,\n2,\n3,\n".split(',\n') // [1,2,3]
"1\n2\n3\n".split('\n') // [1,2,3]

How do i account all three cases into one? Can we sanitize the input somehow and just return the array?

Would really appreciate some help here.

Read more here:

Content Attribution

This content was originally published by TechnoCorner at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: