How to split a string by white space or comma?

Javascript

Javascript Problem Overview


If I try

"my, tags are, in here".split(" ,")

I get the following

[ 'my, tags are, in here' ]

Whereas I want

['my', 'tags', 'are', 'in', 'here']

Javascript Solutions


Solution 1 - Javascript

String.split() can also accept a regular expression:

input.split(/[ ,]+/);

This particular regex splits on a sequence of one or more commas or spaces, so that e.g. multiple consecutive spaces or a comma+space sequence do not produce empty elements in the results.

Solution 2 - Javascript

you can use regex in order to catch any length of white space, and this would be like:

var text = "hoi how     are          you";
var arr = text.split(/\s+/);

console.log(arr) // will result : ["hoi", "how", "are", "you"]

console.log(arr[2]) // will result : "are" 

Solution 3 - Javascript

The suggestion to use .split(/[ ,]+/) is good, but with natural sentences sooner or later you'll end up getting empty elements in the array. e.g. ['foo', '', 'bar'].

Which is fine if that's okay for your use case. But if you want to get rid of the empty elements you can do:

var str = 'whatever your text is...';
str.split(/[ ,]+/).filter(Boolean);

Solution 4 - Javascript

"my, tags are, in here".split(/[ ,]+/)

the result is :

["my", "tags", "are", "in", "here"]

Solution 5 - Javascript

input.split(/\s*[\s,]\s*/)

\s* matches zero or more white space characters (not just spaces, but also tabs and newlines).

... [\s,] matches one white space character or one comma

Solution 6 - Javascript

When I want to take into account extra characters like your commas (in my case each token may be entered with quotes), I'd do a string.replace() to change the other delimiters to blanks and then split on whitespace.

Solution 7 - Javascript

When you need to split a string with some single char delimiters, consider using a reverse logic: match chunks of strings that consist of chars other than the delimiter chars.

So, to extract all chunks of chars other than whitespace (matched with \s) and commas, you can use

console.log("my, tags are, in here".match(/[^\s,]+/g))
// => ["my","tags","are","in","here"]

See the regex demo. String#match extracts all non-overlapping occurrences of one or more (+) chars other than whitespace and comma ([^\s,]).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHoaView Question on Stackoverflow
Solution 1 - JavascriptJonView Answer on Stackoverflow
Solution 2 - JavascriptCemil DoganView Answer on Stackoverflow
Solution 3 - JavascriptjonschlinkertView Answer on Stackoverflow
Solution 4 - JavascriptgabitzishView Answer on Stackoverflow
Solution 5 - JavascriptKaptajnKoldView Answer on Stackoverflow
Solution 6 - JavascriptgrantwparksView Answer on Stackoverflow
Solution 7 - JavascriptWiktor StribiżewView Answer on Stackoverflow