JavaScript split String with white space
You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.
var string = "text to split";
string = string.split(" ");
var stringArray = new Array();
for(var i =0; i < string.length; i++){
stringArray.push(string[i]);
if(i != string.length-1){
stringArray.push(" ");
}
}
Update: Removed trailing space.
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
For split string by space like in Python lang, can be used:
var w = "hello my brothers ;";
w.split(/(\s+)/).filter( function(e) { return e.trim().length > 0; } );
output:
["hello", "my", "brothers", ";"]
or similar:
w.split(/(\s+)/).filter( e => e.trim().length > 0)
(output some)
Using regex:
var str = "my car is red";
var stringArray = str.split(/(\s+)/);
console.log(stringArray); // ["my", " ", "car", " ", "is", " ", "red"]
\s
matches any character that is a whitespace, adding the plus makes it greedy, matching a group starting with characters and ending with whitespace, and the next group starts when there is a character after the whitespace etc.