-2

The confusing behavior of String.split():

When the split function sees a string/character that matches the search string, it would automatically replace the string with a blank string in the returned array:

'aaaaa'.split('a'); // returns ['', '', '', '', '']

But when I put it with other letters, then magically, couple blank strings disappear:

'abababa'.split('a'); // returns ['', 'b', 'b', 'b', '']

I know that it can be used to split words using

'apples are great'.split(' '); // returns ['apples', 'are', 'great']

But what about putting an extra space in between the words?

'apples  are  great'.split(' '); // returns ['apples', '', 'are', '', 'great']

If I wanted apples are great'.split(' '); to return ['apples', '', '', 'are', '', '', 'great'], what are options to make that happen?

Binary
  • 451
  • 5
  • 15
  • 1
    The blank strings don't disappear - they are replaced by the contents that you introduced between the matched instances. – Bergi Feb 21 '19 at 22:32
  • What about matching regions of non-whitespace as well as spaces: `.match(/([^\s]+|\s)/g)` – user3483203 Feb 21 '19 at 22:33
  • 2
    What is your [actual problem](https://meta.stackexchange.com/q/66377)? Your request doesn't really make sense to me. – Bergi Feb 21 '19 at 22:35
  • 1
    Hint: `split` is not confusing at all when you consider the invariant ∀x: `x.split('a').join('a') === x` – Bergi Feb 21 '19 at 22:36
  • 1
    https://stackoverflow.com/questions/12001953/javascript-and-regex-split-string-and-keep-the-separator – epascarello Feb 21 '19 at 22:38
  • 1
    *When the split function sees a string/character that matches the search string, it would automatically replace the string with a blank string in the returned array*: **No**. It returns the characters of the original string between the previous match (or the beginning of the string) and the current match (or the end of the string). The reason why `aaaaa.split('a')` returns `[ "", "", "", "", "", "" ]` (6 strings, not 5 as you stated in your question) is because there are zero characters between each match, and therefore it returns 6 zero-length strings as a result. – p.s.w.g Feb 21 '19 at 22:39

2 Answers2

1

You can refer MDN Docs for split() here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split:

When found, separator is removed from the string and the substrings are returned in an array. If separator is not found or is omitted, the array contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters. If separator appears at the beginning or end of the string, or both, the array begins, ends, or both begins and ends, respectively, with an empty string. Thus, if the string consists solely of one instance of separator, the array consists of two empty strings.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Srijan
  • 257
  • 1
  • 8
0

You could do something like this. Where you match all words and all spaces and then map through them. Using trim will return empty strings if the item was a space.

const res = 'apples  are  great'.match(/\w+|\s/g).map(item=>item.trim());

console.log(res);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • 3
    That was my first take as well, but the OP actually *wants* the empty strings in his result, not avoid them. His problem is that he wants one empty string too much. – Bergi Feb 21 '19 at 22:33
  • Doesn't work, still returns the same `['apples', 'are', 'great']` – Binary Feb 21 '19 at 22:33