Splitting string by whitespace, without empty elements?

前端 未结 5 1388
北荒
北荒 2020-12-24 12:23

I am trying to explode an string using javascript to pick searchterms, whitespace-separated. However I get empty array elements if a searchterm is ended by a whitespace, as

5条回答
  •  余生分开走
    2020-12-24 12:46

    This is a bit old, but for documentation purposes there is also another neat way.

    someString.filter(Boolean);
    
    // Example
    ['fds', '', 'aaaaa', 'AA', 'ffDs', "", 'd'].filter(Boolean);
    // Output
    ["fds", "aaaaa", "AA", "ffDs", "d"]
    

提交回复
热议问题