I’d like to split a sentence into words and the parts between the words (I call them delimiters).
sentence = "First-tea,-then-coffee!"
=> "First-tea,-then-coffee!"
words = sentence.split(/\W+/) # Splits by non-word characters
=> ["First", "tea", "then", "coffee"]
delimiters = sentence.split(/\w+/) # Splits by word characters
=> ["", "-", ",-", "-", "!"]
Splitting into words works fine, but I’m having a question about the delimiters.
Where does this first empty string come from in the delimiter array?
Thanks for explanation.
Between start of line
/^/and first occurrance of-there is"First".So it splits on
"First"obtaining an empty string""and-.