You can just split on the word boundary using \b
. See MDN
"\b: Matches a zero-width word boundary, such as between a letter and a space."
You should also make sure it is followed by whitespace \s
. so that strings like "My car isn't red"
still work:
var stringArray = str.split(/\b(\s)/);
The initial \b
is required to take multiple spaces into account, e.g. my car is red
EDIT: Added grouping