Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么javascript在创建url对象时会忽略url字符串的端口?_Javascript_Url_Https_Port - Fatal编程技术网

为什么javascript在创建url对象时会忽略url字符串的端口?

为什么javascript在创建url对象时会忽略url字符串的端口?,javascript,url,https,port,Javascript,Url,Https,Port,我想读取我拥有的url字符串的端口。我发现可以使用urlstring作为参数创建一个新的URL对象。然后,您可以调用该对象的port属性来获取urlstring的端口 我的url的端口是443。如果我将443作为端口的字符串输入URL对象,则URL对象的端口属性为“”。如果我选择其他数字作为端口,它可以正常工作 有人知道为什么会这样吗 以下是一段代码片段: const URL\u STRING1=”https://example.com:443/"; 设url1=新URL(URL\u STRI

我想读取我拥有的
url
字符串的端口。我发现可以使用
urlstring
作为参数创建一个新的URL对象。然后,您可以调用该对象的port属性来获取
urlstring
的端口

我的
url
的端口是
443
。如果我将443作为端口的字符串输入URL对象,则
URL对象的端口属性为
“”
。如果我选择其他数字作为端口,它可以正常工作

有人知道为什么会这样吗

以下是一段代码片段:

const URL\u STRING1=”https://example.com:443/";
设url1=新URL(URL\u STRING1);
console.log(url1.port);
常量URL_STRING2=”https://example.com:442/";
设url2=新URL(URL_STRING2);
console.log(url2.port);
常量URL_STRING3=”https://example.com:444/";
设url3=新URL(URL\u STRING3);

console.log(url3.port)
443
https



这个物体是实验性的,所以你可以在上面试一下。使用简单的正则表达式提取端口-

const URL_STRING1 = "https://example.com:443/";
console.log(URL_STRING1.match(/https:\/\/example\.com:(?<=:)(\d{3})/));
const URL\u STRING1=”https://example.com:443/";
log(URL\u STRING1.match(/https:\/\/example\.com:(?)?
A special scheme is a scheme listed in the first column of the following table. A default port is a special scheme’s optional corresponding port and is listed in the second column on the same row.

scheme  port
"ftp"   21
"file"  
"http"  80
"https"     443
"ws"    80
"wss"   443 
const URL_STRING1 = "https://example.com:443/";
console.log(URL_STRING1.match(/https:\/\/example\.com:(?<=:)(\d{3})/));