Last active
October 22, 2015 01:29
-
-
Save petermoresi/bbe22f4340d22836976d to your computer and use it in GitHub Desktop.
Convert mixed whitespace and comma into array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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