Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我这样做时"www.example.com/test/test".gsub('/test',''),我得到"www.example.com"了,但我想要的结果是"www.example.com/test"。也就是说,只有最后一个"/test"应该被删除。我们如何实现它?
"www.example.com/test/test".gsub('/test','')
"www.example.com"
"www.example.com/test"
"/test"
采用"www.example.com/test/test".gsub(/\/test$/,'')
"www.example.com/test/test".gsub(/\/test$/,'')
评论后更新
事件如果它正在使用gsub,sub似乎更合适,因为您只想更换一次:
gsub
sub
"www.example.com/test/test".sub(/\/test$/,'')
试试这个
"www.example.com/test/test".sub('/test','')