假設你有一個這樣的字串:
"This is my.linkhttps://google.com/1two3!!!"
你想得到確切的資訊:
https://google.com/1two3
試過:
message.split.select{ |m| m.include?("google") }
但它不會拆分期間。所以 ti 包括my.link和!!!。
message.split(/[\s.] /)
但它會拆分鏈接本身。
uj5u.com熱心網友回復:
我會使用一個可以容納任何 url 的正則運算式:
"This is my.linkhttps://google.com/1two3!!!".match(/https:[\/\w\.]*/)[0]
#=> "https://google.com/1two3"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/527874.html
