Skip to content

Instantly share code, notes, and snippets.

@petermoresi
Last active October 22, 2015 01:29
Show Gist options
  • Save petermoresi/bbe22f4340d22836976d to your computer and use it in GitHub Desktop.
Save petermoresi/bbe22f4340d22836976d to your computer and use it in GitHub Desktop.
Convert mixed whitespace and comma into array
// Let's say you want a text field on a webpage
// where the user can enter a mix of comma delimited,
// white space delimited and line delimited.
//
// BUT you want the computer to read it as a flat list
var str = "1,2\n3 4";
str.replace(/,\W/g, ' ')
.replace(/,/g, ' ')
.replace(/\n/g, ' ')
.replace(/\W+/g, ' ').split(' ');
// str.should.be(["1", "2", "3", "4"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment