PHP's "parse_url" in JavaScript
PHP's "parse_url" in JavaScript
我编写了PHP的parse_url函数的JavaScript副本。
就我现在所写的而言,函数完成了任务并返回了一个JS对象。。。
但是有更好的方法吗:
function parse_url( url ) {
if(typeof url == 'string') {
// output following: scheme, host, user, pass, path, query, fragment
var output = {};
var split_scheme = url.split('//');
// now we assume that we have: sheme, and the rest of the url after //
if(split_scheme.length == 2) {
// now we have the "scheme"
// do not add if this URL is provided: //hostname/path?query=value#anchor
if(split_scheme[0].length) {
output.scheme = split_scheme[0].replace(':', '');
}
// we're now splitting the URL on first slash /
// and assume that we'll get: host, (user and pass, if any);
var split_url = split_scheme[1].split('/');
if(split_url.length == 2) {
// check if user/pass are provided
var split_auth_hostname = split_url[0].split('@');
output.host = split_auth_hostname[1];
if(split_auth_hostname.length == 2) {
// now split the auth part of the hostname with ":"
var split_user_info = split_auth_hostname[0].split(':');
if(split_user_info.length == 2) {
// assume that both user and pass are provided now
output.user = split_user_info[0];
output.pass = split_user_info[1];
} else {
// assume that only "user" is provided
output.user = split_user_info[0];
}
} else {
// assume that no auth info was provided in the URL
// first splitted element is the actual hostname
output.host = split_auth_hostname[0];
}
// now let's split the query/anchor from path
var split_query = split_url[1].split('?');
output.path = '/' + split_query[0];
if(split_query.length == 2) {
// now split the anchor out of query string
var split_anchor = split_query[1].split('#');
// add the query without anchor
output.query = split_anchor[0];
// add anchoer
if(split_anchor.length == 2) {
output.fragment = '#' + split_anchor[1];
}
} else {
output.query = split_query[0];
}
}
}
return output;
}
}
我在这里创建了一个演示jsfiddle。
今天您可以使用new URL('https://stackoverflow.com/questions')
https://developer.mozilla.org/en-US/docs/Web/API/URL
console.log(new URL('https://www.example.com/dogs#start'));
{
hash: "start"
host: "www.example.com"
hostname: "www.example.com"
href: "http://www.example.com/dogs"
origin: "http://www.example.com"
password: ""
pathname: "/dogs"
port: ""
protocol: "https:"
search: ""
searchParams: URLSearchParams { }
username: ""
}
我使用了javascript中的正则表达式的好部分。这是一个教科书式的例子,它的运行速度比您的代码快73%,所以希望生产质量的代码在最坏的情况下做得更好。
基准的URLhttp://jsperf.com/parse-url.
使用的代码是:
function parse_url_regex(url) {
var parse_url = /^(?:([A-Za-z]+):)?('/{0,3})([0-9.'-A-Za-z]+)(?::('d+))?(?:'/([^?#]*))?(?:'?([^#]*))?(?:#(.*))?$/;
var result = parse_url.exec(url);
return result;
}
但我强烈建议您尝试codereview.stackeexchange.com
相关文章:
- 强制URL字段不具有“;http://www."使用regex
- 如何使用Javascript获取当前页面URL,并在<a href="[the_URL]”>
- "访问被拒绝”;当试图在IE11中打开为程序生成的PDF生成的URL时
- "“用户友好”;vimeo嵌入,从url获取嵌入代码
- tinyMCE删除背景图像:“"类似于magento的url的内容
- Linkify/Clickable Text URL,但忽略那些已经包装在“;a href"'s
- "/"当onclick事件时从URL中删除
- 阻止href="#"链接更改URL哈希
- "未定义的“;以及“;高速缓存/xxx”;已附加到URL
- 参考“;href"而不是html中锚点标签中的url
- 当内联链接“#"被点击或输入到url中,就像链接到reddit评论时一样
- 更改URL而不使用<a href="">
- 华丽的弹出窗口:获得"无法加载图像“;并且图像url未定义
- "不安全的JavaScript尝试访问URL为..的帧"使用本地文件
- 我怎么能在一个URL中有两种类型的参数,其中一种以“”开头#"而另一个以“0”开头&"并且不替
- 检查“/"在URL上使用jQuery
- 什么's使用“:"在URL路径中的名称之前
- 如何在Javascript代码中使用变量Ex:src="htp://www.url.com/file.php?
- ""提交的url中的字符
- HTML元素"中的URL文本.Split不是一个函数