JavaScript split String with white space

JavascriptArraysSplit

Javascript Problem Overview


I would like to split a String but I would like to keep white space like:

var str = "my car is red";

var stringArray [];

stringArray [0] = "my";
stringArray [1] = " ";
stringArray [2] = "car";
stringArray [3] = " ";
stringArray [4] = "is";
stringArray [5] = " ";
stringArray [6] = "red";

How I can proceed to do that?

Thanks !

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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)

Solution 4 - Javascript

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

Solution 5 - Javascript

Although this is not supported by all browsers, if you use capturing parentheses inside your regular expression then the captured input is spliced into the result.

> If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. [reference)

So:

var stringArray = str.split(/(\s+)/);
                             ^   ^
//

Output:

["my", " ", "car", " ", "is", " ", "red"]

This collapses consecutive spaces in the original input, but otherwise I can't think of any pitfalls.

Solution 6 - Javascript

In case you're sure you have only one space between two words, you can use this one

str.replace(/\s+/g, ' ').split(' ')

so you replace one space by two, the split by space

Solution 7 - Javascript

str.split(' ').join('§ §').split('§');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoseView Question on Stackoverflow
Solution 1 - JavascriptchridamView Answer on Stackoverflow
Solution 2 - JavascriptsomethinghereView Answer on Stackoverflow
Solution 3 - JavascriptiqmakerView Answer on Stackoverflow
Solution 4 - JavascriptRhumborlView Answer on Stackoverflow
Solution 5 - JavascriptLightness Races in OrbitView Answer on Stackoverflow
Solution 6 - Javascriptuser7153178View Answer on Stackoverflow
Solution 7 - JavascriptEsgerView Answer on Stackoverflow