How do I split a string by whitespace and ignoring leading and trailing whitespace into an array of words using a regular expression?

JavascriptRegexStringWhitespaceRemoving Whitespace

Javascript Problem Overview


I typically use the following code in JavaScript to split a string by whitespace.

"The quick brown fox jumps over the lazy dog.".split(/\s+/);
// ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]

This of course works even when there are multiple whitespace characters between words.

"The  quick brown fox     jumps over the lazy   dog.".split(/\s+/);
// ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]

The problem is when I have a string that has leading or trailing whitespace in which case the resulting array of strings will include an empty character at the beginning and/or end of the array.

"  The quick brown fox jumps over the lazy dog. ".split(/\s+/);
// ["", "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog.", ""]

It's a trivial task to eliminate such empty characters, but I'd rather take care of this within the regular expression if that's at all possible. Does anybody know what regular expression I could use to accomplish this goal?

Javascript Solutions


Solution 1 - Javascript

If you are more interested in the bits that are not whitespace, you can match the non-whitespace instead of splitting on whitespace.

"  The quick brown fox jumps over the lazy dog. ".match(/\S+/g);

Note that the following returns null:

"   ".match(/\S+/g)

So the best pattern to learn is:

str.match(/\S+/g) || []

Solution 2 - Javascript

" The quick brown fox jumps over the lazy dog. ".trim().split(/\s+/);

Solution 3 - Javascript

Instead of splitting at whitespace sequences, you could match any non-whitespace sequences:

"  The quick brown fox jumps over the lazy dog. ".match(/\S+/g)

Solution 4 - Javascript

Not elegant as others code but very easy to understand:

    countWords(valOf)
    {
        newArr[];
        let str = valOf;
        let arr = str.split(" ");
   
        for (let index = 0; index < arr.length; index++) 
       {
           const element = arr[index];
           if(element)
           {
              newArr.push(element);
           }
       }
       const NumberOfWords = newArr.length;
     
       return NumberOfWords;
   }

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
Questionnatlee75View Question on Stackoverflow
Solution 1 - JavascriptkennebecView Answer on Stackoverflow
Solution 2 - JavascriptJoshView Answer on Stackoverflow
Solution 3 - JavascriptGumboView Answer on Stackoverflow
Solution 4 - JavascriptarisView Answer on Stackoverflow