I’m trying to write a function to split a word into letters, and I’m getting a weird result:
'abc'.split(/(a|b|c)/)
Gives:
["", "a", "", "b", "", "c", ""]
Incidentally, I see the same results in Python, so clearly the problem is me!
>>> re.split( '(a|b|c)', 'abc')
['', 'a', '', 'b', '', 'c', '']
The problem is, why are there empty strings intercalated between the letters? I expected
["a", "b", "c"]
Thanks!
Instead of splitting, you can do a string match with a global regex…
This makes more sense since all you really care about is the match, not what’s between the match.
If you wanted to match any character from a to z, you can do this…
I made it case insensitive to match upper case too.