79

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?

natlee75
  • 5,097
  • 3
  • 34
  • 39
  • Horses for courses. `split` is used to *split* a string, not *mutate* it. See [how to trim a string in JavaScript?](http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript). – DCoder Feb 16 '13 at 16:37
  • unfortunately javascript doesnt support **lookbehind** and even if you had used **lookbehind**,there would be space in the first split – Anirudha Feb 16 '13 at 16:40
  • I never thought of it from that perspective. Thanks for pointing that out! – natlee75 Feb 17 '13 at 00:05
  • can't you do a trim() before the split? – Jacques Koorts May 18 '18 at 11:33

4 Answers4

124

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) || []
Andy
  • 7,885
  • 5
  • 55
  • 61
kennebec
  • 102,654
  • 32
  • 106
  • 127
51

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

Josh
  • 3,264
  • 1
  • 23
  • 35
  • 3
    Thanks for the suggestion. I actually was going to go this route until I remembered that it requires a browser that supports JavaScript 1.8. That's fine for a majority of our users, but we still support older browsers such as Internet Explorer 7 and 8 whose JavaScript engines don't include this functionality. – natlee75 Feb 17 '13 at 00:09
  • 11
    Watch out, `' '.trim().split(/\s+/)` returns `[""]`! – Andy Jun 14 '16 at 21:39
  • I wonder whose solution is faster yours (if we handle the [""] case) or @kennebec 's – ibodi Jun 09 '19 at 17:22
16

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)
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

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;
   }
aris
  • 409
  • 6
  • 8