从 javascript 字符串中删除 http 或 https
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3999764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
taking off the http or https off a javascript string
提问by Matt Elhotiby
I have the following strings
我有以下字符串
http://example.com
https://example.com
http://www.example.com
how do i get rid of the http://or https://?
我如何摆脱http://或https://?
回答by ncardeli
Try with this:
试试这个:
var url = "https://site.com";
var urlNoProtocol = url.replace(/^https?\:\/\//i, "");
回答by Bick
var txt="https://site.com";
txt=/^http(s)?:\/\/(.+)$/i.exec(txt);
txt=txt[2];
for parsing links without http/https use this:
要解析没有 http/https 的链接,请使用:
var txt="https://site.com";
txt=/^(http(s)?:\/\/)?(.+)$/i.exec(txt);
txt=txt[3];
回答by Vincent D'amour
回答by user113716
var str = "https://site.com";
str = str.substr( str.indexOf(':') + 3 );
Instead of .substr(), you could also use .slice()or .substring(). They'll all produce the same result in this situation.
代替.substr(),您也可以使用.slice()或.substring()。在这种情况下,它们都会产生相同的结果。
str = str.slice( str.indexOf(':') + 3 );
str = str.substring( str.indexOf(':') + 3 );
EDIT:It appears as though the requirements of the question have changed in a comment under another answer.
编辑:似乎问题的要求在另一个答案下的评论中发生了变化。
If there possibly isn'ta http://in the string, then do this:
如果字符串中可能没有a http://,请执行以下操作:
var str = "site.com";
var index = str.indexOf('://');
if( index > -1 )
str = str.substr( index + 3 );
回答by Green
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:
您可以使用URL() 构造函数。它将解析您的 url 字符串,并且会有一个没有协议的条目。所以用正则表达式少头疼:
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 wwwpart. In my case I wanted to get rid of that subdomain part as well, so had to use to .replace()anyway.
虽然URL()放弃了协议,但它让你有www一部分。在我的情况下,我也想摆脱那个子域部分,所以.replace()无论如何都必须使用。
u.host.replace(/^www./, '') // www.facebook.com => facebook.com
回答by yowainwright
This answer extends some answers above, http://, https://, or//which is also common.
这个答案扩展了上面的一些答案, http://, https://,或者//这也很常见。
Thanks for answers above that led me to this!
感谢上面的答案让我做到了这一点!
const urls = [ "http://example.com", "https://example.com", "//example.com" ]
// the regex below states: replace `//` or replace `//` and the 'stuff'
const resolveHostNames = urls.map(url => url.replace(/\/\/|.+\/\//, ''))
console.log(resolveHostNames);
Here's a link to a codepen.
回答by Rahul
Another efficient solution,
另一个有效的解决方案,
url.replace(/(^(\w+:)?\/\//, '')
url.replace(/(^(\w+:)?\/\//, '')
回答by Evgeny
Strip the protocol from a URL:
从 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).
适用于任何协议,ftp、http、gopher、nntp、telnet、wais、file、prospero ... RFC 1738 中指定的所有协议,但其中没有 // 的协议除外(mailto、news)。
回答by UpTheCreek
Assuming there are no double slashes other than the protocol, you could do:
假设除了协议没有双斜线,你可以这样做:
var url = "https://example.com";
var noProtocol = url.split('//')[1];
回答by ilyaigpetrov
You may use HTMLHyperlinkElementUtilsof DOM:
你可以使用DOM 的HTMLHyperlinkElementUtils:
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:
来自MDN 上的 HTMLHyperlinkElementUtils:
a.hostname, example.coma.host, example.com:3000a.pathname, /foo/bar.htmla.search, ?a=1&b=2a.hash, #gooa.username, a.password, a.port, etc.
a.hostname,example.coma.host,example.com:3000a.pathname,/foo/bar.htmla.search,?一个= 1&B = 2a.hash,#gooa.username,a.password,a.port等。

