taking off the http or https off a javascript string

前端 未结 12 1580
面向向阳花
面向向阳花 2020-12-20 11:23

I have the following strings

http://example.com
https://example.com
http://www.example.com

how do i get rid of the http:// or

相关标签:
12条回答
  • 2020-12-20 11:52

    You may use HTMLHyperlinkElementUtils of DOM:

    function removeProtocol(url) {
      const a = document.createElement('a');
      a.href = url;
      // `url` may be relative, but `a.href` will be absolute.
      return a.href.replace(a.protocol + '//', '');
    }
    
    removeProtocol('https://example.com/https://foo');
    // 'example.com/https://foo'
    
    removeProtocol('wrong://bad_example/u');
    // 'bad_example/u'
    

    From HTMLHyperlinkElementUtils on MDN:

    a.hostname, example.com
    a.host, example.com:3000
    a.pathname, /foo/bar.html
    a.search, ?a=1&b=2
    a.hash, #goo
    a.username, a.password, a.port, etc.

    0 讨论(0)
  • 2020-12-20 11:53

    Strip the protocol from a URL:

    var url = "https://site.com";
    var urlNoProto = url.split('/').slice(2).join('/');
    

    Works with any protocol, ftp, http, gopher, nntp, telnet, wais, file, prospero ... all those specified in RFC 1738 with the exception of those without a // in them (mailto, news).

    0 讨论(0)
  • 2020-12-20 11:56

    Another efficient solution,

    url.replace(/(^(\w+:)?\/\//, '')

    0 讨论(0)
  • 2020-12-20 12:02

    Please note that in real web pages inherited protocol // is a common practice https://paulirish.com/2010/the-protocol-relative-url.

    So I suggest regexp covering this case as well:

    /^\/\/|^https?:\/\//

    (you can optimize it)

    0 讨论(0)
  • 2020-12-20 12:04

    Try with this:

    var url = "https://site.com";
    var urlNoProtocol = url.replace(/^https?\:\/\//i, "");
    
    0 讨论(0)
  • 2020-12-20 12:06

    You may use URL() constructor. It will parse your url string and there will be an entry w/o protocol. So less headache with regexps:

    let u = new URL('https://www.facebook.com/companypage/');
    URL {
        hash: ""
        host: "www.facebook.com"
        hostname: "www.facebook.com"
        href: "https://www.facebook.com/companypage/"
        origin: "https://www.facebook.com"
        password: ""
        pathname: "/companypage/"
        port: ""
        protocol: "https:"
        search: ""
        searchParams: URLSearchParams {}
        username: ""
    }
    u.host // www.facebook.com
    u.hostname // www.facebook.com
    

    Although URL() drops out a protocol, it leaves you with www part. In my case I wanted to get rid of that subdomain part as well, so had to use to .replace() anyway.

    u.host.replace(/^www./, '') // www.facebook.com => facebook.com
    
    0 讨论(0)
提交回复
热议问题