我试图在 javascript 中解决这个问题,应该由以下人员处理:
var url = new URL('http://a:b@example.com:890/path/wah@t/foo.js?foo=bar&bingobang=&king=kong@kong.com#foobar/bing/bo@ng?bang');
因为(至少在 Chrome 中)它解析为:
{
"hash": "#foobar/bing/bo@ng?bang",
"search": "?foo=bar&bingobang=&king=kong@kong.com",
"pathname": "/path/wah@t/foo.js",
"port": "890",
"hostname": "example.com",
"host": "example.com:890",
"password": "b",
"username": "a",
"protocol": "http:",
"origin": "http://example.com:890",
"href": "http://a:b@example.com:890/path/wah@t/foo.js?foo=bar&bingobang=&king=kong@kong.com#foobar/bing/bo@ng?bang"
}
但是,这不是跨浏览器 (https://developer.mozilla.org/en-US/docs/Web/API/URL),所以我将其拼凑在一起,以提取与上述相同的部分:
^(?:(?:(([^:\/#\?]+:)?(?:(?:\/\/)(?:(?:(?:([^:@\/#\?]+)(?:\:([^:@\/#\?]*))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((?:\/?(?:[^\/\?#]+\/+)*)(?:[^\?#]*)))?(\?[^#]+)?)(#.*)?
这个正则表达式的功劳归于 https://gist.github.com/rpflorence,他发布了这个 jsperf http://jsperf.com/url-parsing(最初在这里找到:https://gist.github.com/jlong/2428561#comment-310066),他提出了这个最初基于的正则表达式。
零件顺序如下:
var keys = [
"href", // http://user:pass@host.com:81/directory/file.ext?query=1#anchor
"origin", // http://user:pass@host.com:81
"protocol", // http:
"username", // user
"password", // pass
"host", // host.com:81
"hostname", // host.com
"port", // 81
"pathname", // /directory/file.ext
"search", // ?query=1
"hash" // #anchor
];
还有一个封装了它并提供查询参数的小库:
https://github.com/sadams/lite-url(也可以在凉亭上找到)
如果您有改进,请创建一个包含更多测试的拉取请求,我会接受并合并,谢谢。