Remove “www”, “http://” from string

前端 未结 3 1839
野性不改
野性不改 2021-01-02 01:47

How can I remove \"www\", \"http://\", \"https://\" from strings using Ruby?

I tried this but it didn\'t work:

s.gsub(\'/(?:http?:\\/\\/)?(?:www\\.)?         


        
3条回答
  •  别那么骄傲
    2021-01-02 02:24

    def strip_url(target_url)
      target_url.gsub("http://", "")
                .gsub("https://", "")
                .gsub("www.", "")
    end
    
    strip_url("http://www.google.com")
     => "google.com" 
    strip_url("https://www.google.com")
     => "google.com" 
    strip_url("http://google.com")
     => "google.com"
    strip_url("https://google.com")
     => "google.com" 
    strip_url("www.google.com")
     => "google.com" 
    

提交回复
热议问题