好的。這很晚了,我很累。Rails中的簡單正則表達式問題
我想匹配字符串中的字符。具體來說就是'a'的出現。就像「一個半」一樣。
如果我有一個全是小寫的字符串。
"one and a half is always good" # what a dumb example. No idea how I thought of that.
我呼籲它
"one and a half is always good".titleize #=> "One And A Half Is Always Good"
titleize這是錯誤的,因爲 '和' 和 'A' 應該是小寫。明顯。
所以,我能做的
"One and a Half Is always Good".titleize.tr('And', 'and') #=> "One and a Half Is always Good"
我的問題:我該如何使「A」的「一」,也沒有留下「始終」到「永遠」?
\ BA \ b 的\ b詞邊界一致。你正在尋找正則表達式,對吧? – 2010-12-18 07:10:35
是的!那麼它將如何工作? – 2010-12-18 07:36:20
''一個半是總是好的'.gsub(/ \ bA \ b /,'a')#=>「一個半永遠是好的」 – 2010-12-18 08:11:38