Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on newline and comma [duplicate]

My input String is like

abc,def,wer,str

Currently its splitting only on comma but in future it will contain both comma and newline. Current code as below:

$scope.memArray = $scope.memberList.split(",");

In future I need to split on both comma and newline what should be the regex to split both on comma and newline. I tried - /,\n\ but its not working.

like image 915
Bidisha Avatar asked Dec 16 '15 15:12

Bidisha


3 Answers

You can use a regex:

var splitted = "a\nb,c,d,e\nf".split(/[\n,]/);
document.write(JSON.stringify(splitted));

Explanation: [...] defines a "character class", which means any character from those in the brackets.

p.s. splitted is grammatically incorrect. Who cares if it's descriptive though?

like image 120
Merott Avatar answered Nov 20 '22 06:11

Merott


You could replace all the newlines with a comma before splitting.

$scope.memberList.replace(/\n/g, ",").split(",")
like image 27
user5325596 Avatar answered Nov 20 '22 05:11

user5325596


Try

.split(/[\n,]+/)

this regex should work.

like image 6
alek kowalczyk Avatar answered Nov 20 '22 06:11

alek kowalczyk