ruby - Lossy Split and Join Expression -


i'm using split(/ /) method, after join them ending white spaces gone.

the string matched against this:

"word word  word  " 

the array returned split(/ /) this:

["word", " ", "word", " ", " ", "word"] 

i expected this:

["word", " ", "word", " ", " ", "word", " ", " "] 

this should work you

str.scan(/\w+| /)  

this example

2.2.1 :003 > "word word word ".scan(/\w+| /)
=> ["word", " ", "word", " ", " ", "word", " "]


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -