171

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']
Alexander Abakumov
14.7k16 gold badges99 silver badges135 bronze badges
asked Apr 27, 2012 at 7:44
2
  • 6
    don't you mean whitespace or comma? Commented Apr 27, 2012 at 7:52
  • 1
    As an explanation for the result that you're getting: "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. Commented Sep 20, 2016 at 7:27

7 Answers 7

323

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.

TJBlackman
2,3935 gold badges28 silver badges56 bronze badges
answered Apr 27, 2012 at 7:46
Sign up to request clarification or add additional context in comments.

6 Comments

@Bergi: Well, it's both more strict than what I suggest (only one comma allowed, in front) and more loose (split on all whitespace) than what the OP asked for. IMHO it would be simply worse -- consider the input spaces , before commas.
@Jon: OK, that depends on the OPs needs. I wouldn't plenk :-)
+1 I know this is a little old but why use a blank space and not \s. I may have some line breaks in the blob and \s takes care of those too.
FACEPALM NOTE: don't put quotes around the regex. e.g. Don't use 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(\"", ")\""]).
input.split(/[ ,]+/);
|
73

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" 
Paul Rooney
21.7k9 gold badges47 silver badges64 bronze badges
answered Sep 22, 2015 at 22:06

1 Comment

Beware leading/trailing whitespace when using /\s+/. For example, 'a b c '.split(/\s+/) === [ 'a', 'b', 'c', '' ]. If you .trim() the string first, you'll be good.
49

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);
answered May 19, 2014 at 2:08

6 Comments

Thats a very clever use of native object implicit constructors- my computers keyboard is mad this morning- I'll edit this comment later- but point is invoking Boolean like 'Boolean()' will construct a new instance of [object Boolean] with a value of false, just as would invoking 'new Boolean()'. That will filter out all matches down to this default behaviour. Nice one :)
what exactly do you mean by "natural sentences"? I couldn't emulate it nor do I understand what this is supposed to do.
It's explained by @VLostBoy. When the 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.
btw, you can use implicit constructors for other similar fun stuff, like [1, 2, 3].map(String)
"foo, bar,,foobar,".split(/[\s,]+/) returns ["foo", "bar", "foobar", ""] (because of the dangling comma at the end), thanks!
|
13
"my, tags are, in here".split(/[ ,]+/)

the result is :

["my", "tags", "are", "in", "here"]
answered Apr 27, 2012 at 7:47

Comments

13

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

answered Apr 27, 2012 at 7:50

8 Comments

Nop. That's not good. This is the output : ["my", "tags are", "in here"]
it seems to split on each character.
@Marco Oops. Probably should have tested it before I made that last edit. I have now, and this time it really should work.
@KaptajnKold Oh, I didn't catch that, thanks for answering!
@leonheess I'm embarrassed to say that you're right. I've removed the part of the answer that was wrong. Sheesh! This answer has not been my finest work. I really should learn to test things.
|
3

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.

answered Jun 21, 2013 at 22:37

1 Comment

str_variable.replace(/[,'"]+/gi, ' ').split(' ')
1

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,]).

answered Oct 5, 2021 at 22:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.