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

Problem:

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

Context:

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: https://stackoverflow.com/questions/68489458/combining-multiple-delimiters-split-in-javascript

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: