Split a string using whitespace in Javascript?

You probably don't even need to filter, just split using this Regular Expression:

"   I dont know what you mean by glory Alice said.".split(/\b\s+/)

 str.match(/\S+/g) 

returns a list of non-space sequences ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said."] (note that this includes the dot in "said.")

 str.match(/\w+/g) 

returns a list of all words: ["I", "dont", "know", "what", "you", "mean", "by", "glory", "Alice", "said"]

docs on match()


You should trim the string before using split.

var str = " I dont know what you mean by glory Alice said."
var trimmed = str.replace(/^\s+|\s+$/g, '');
trimmed = str.split(" ")

I recommend .match:

str.match(/\b\w+\b/g);

This matches words between word boundaries, so all spaces are not matched and thus not included in the resulting array.