Quantcast
Viewing latest article 13
Browse Latest Browse All 56

Splitting string by whitespace, without empty elements?

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 shown below.

What should I do instead to avoid post-processing this array and removing empty elements?

var str = "searchterm1 searchterm2";
console.log(str.split(" ")); // ["searchterm1", "searchterm2"]

var strb = "searchterm1 "; // Note the ending whitespace
console.log(strb.split(" ")); // ["searchterm1", ""]

Viewing latest article 13
Browse Latest Browse All 56

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>