2
爲什麼第二個split
在下面返回標點符號?爲什麼在正則表達式中使用圓括號會更改輸出?使用正則表達式的`String#split`方法的不同返回值
str = "This is a test string. Let's split it."
str.split(/\. /)
# =>["This is a test string", "Let's split it."]
str.split(/(\.)/)
# =>["This is a test string", ". ", "Let's split it."]
它與Ruby無關。這是正則表達式的行爲。同樣在Perl – texasbruce
Aaaaah這和Yu的回答有很大的意義。謝謝。 – aceofbassgreg
@texasbruce,它與Ruby有關。 'String#split()'被定義爲這樣做。 Ruby從perl中借用了很多東西,所以perl也定義了split()來做到這一點。 – 7stud