为什么 javascript 在创建 url-Object 时忽略 url-string 的端口?

Why does javascript ignore the port of an url-string when creating the url-Object?

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

的端口

我的url端口是443。如果我将带有 443 的字符串作为 URL-对象的端口,则 url-object 的端口-属性 为 ""。如果我选择其他数字作为端口,它工作正常。

有人知道为什么会这样吗?

这是一个代码片段:

const URL_STRING1 = "https://example.com:443/";

let url1 = new URL(URL_STRING1);
console.log(url1.port);

const URL_STRING2 = "https://example.com:442/";

let url2 = new URL(URL_STRING2);
console.log(url2.port);

const URL_STRING3 = "https://example.com:444/";

let url3 = new URL(URL_STRING3);
console.log(url3.port);

443https

的默认端口

URL reference for full specs

....
--->   Set url’s port to null, if port is url’s scheme’s default port, and to port otherwise.
                                                ^^^^^^^^^^^^^^^^^^^^^
....

Default scheme reference for full specs

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})/));