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']
7 Answers 7
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.
6 Comments
spaces , before commas.\s. I may have some line breaks in the blob and \s takes care of those too.input.split("/[ ,]+/)". Leave the quotes out (input.split(//) instead of input.split("//")) and you'll have a much better experience. Because oddly, that would really probably only work on itself (to generate ["input.split(\"", ")\""]).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"
1 Comment
/\s+/. For example, 'a b c '.split(/\s+/) === [ 'a', 'b', 'c', '' ]. If you .trim() the string first, you'll be good.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);
6 Comments
Boolean() constructor is called on any value, it casts that value to a boolean - true or false. Thus, any falsy values will be filtered from the array, including empty strings.[1, 2, 3].map(String)"foo, bar,,foobar,".split(/[\s,]+/) returns ["foo", "bar", "foobar", ""] (because of the dangling comma at the end), thanks!"my, tags are, in here".split(/[ ,]+/)
the result is :
["my", "tags", "are", "in", "here"]
Comments
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
8 Comments
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.
1 Comment
str_variable.replace(/[,'"]+/gi, ' ').split(' ')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,]).
"my, tags are, in here".split(" ,")will split the string only where a space followed by a comma is the separator. Your string does not contain that sequence, hence it is not splitted."my, tags are, in here".split(", ")with the splitting sequence swapped will at least split your original string in three parts, after each comma-and-space. If you do want five parts, the answers below specify the match string as a regular expression matching a space or a comma.