EagerToLearn
EagerToLearn

Reputation: 675

Explanation on how string.split works

I'm very sorry if this is a silly question, but why does this return 5 :

"2222".split(/\d/).length

"2222".split(/\d/) will return one array consists of 5 empty string, why is that?

I understand \dstands for one digit. I also understand if this return an arrary consists of 4 "a" string :

"a a a a".split(" ")

But I can't get my head around the first example above. Please give me an explanation.

Upvotes: 1

Views: 79

Answers (2)

Welbog
Welbog

Reputation: 60398

Split is going to find the first instance of the string/regex you're splitting on and cut the string at that instance, removing it and leaving behind whatever was before the cut as the first result. It will continue to do this for the entire input string.

An example:

"abcbbd".split(/b/)
// Results in ["a", "c", "", "d"]

We start by looking for a substring that matches /b/ in the string. We find one at index 1, length 1: abcbbd. Our starting index is 0, so we take the substring(0,1) from the main string, and that's the first element: ["a"].

Now the starting index is 2 (the end of the string we matched, which was /b/) and we start again, finding /b/ at index 3, length 1: abcbbd. We chop the string again, now the result is ["a", "c"].

Now the starting index is 4. We found /b/ again immediately, index 4, length 1: abcbbd. We chop the string, from the starting index 4 to the current index 4, yielding an empty string. Our result is now ["a", "c", ""].

Continuing to the end of the string, we get ["a", "c", "", "d"].

Caveat: Yes, this isn't exactly how split works, but for the purposes of understanding its output, it is sufficient.

Upvotes: 1

James Thorpe
James Thorpe

Reputation: 32202

Let's imagine instead your string was:

" 2 2 2 2 "

And you split by digits (ie the 2) - you'd end up with an array of 5 strings consisting of a single space. Remove all those spaces and continue to split by digits - you have 5 strings from the same positions/boundaries before and after each 2 as per the original string, but they're now empty.

Upvotes: 3

Related Questions