ruby - Different return values of `String#split` method using regex -


why second split in following return punctuation? why using parentheses in regular expression change output?

str = "this test string. let's split it."  str.split(/\. /) # =>["this test string", "let's split it."]  str.split(/(\. )/) # =>["this test string", ". ", "let's split it."] 

because second code uses regex contains group. string#split:

if pattern regexp, str divided pattern matches. whenever pattern matches zero-length string, str split individual characters. if pattern contains groups, respective matches returned in array well.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -