admin管理员组文章数量:1130349
parse方法
parse方法将url解析为对象,第一个参数是要解析成对象的url字符串,第二个是是否查询参数,为true,查询参数解析为对象,第三个是是否以//解析主机
const path = require("path");
const url=require("url");let str="/images/fff/123/jj.jpg";
console.log(path.parse(str));
结果:
{root: '/',dir: '/images/fff/123',base: 'jj.jpg',ext: '.jpg',name: 'jj'
}console.log(path.sep);// \
let u = "//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash";
console.log(url.parse(u));//query结果:
\
Url {protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: '#hash',search: '?id=1&name=tom',query: 'id=1&name=tom',pathname: '//www.baidu:8080/images/fff/123/jj.jpg',//pathname 属性是一个可读可写的字符串,可设置或返回当前 URL 的路径部分path: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u,true));Url {protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: '#hash',search: '?id=1&name=tom',query: [Object: null prototype] { id: '1', name: 'tom' },//第二个参数为true,query属性就会从查询字符串格式(“a=1&b=2”)转换为了对象格式({a: 1,b: 2})pathname: '//www.baidu:8080/images/fff/123/jj.jpg',path: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u, true, true));
Url {protocol: null,slashes: true,auth: null,host: 'www.baidu:8080',//hostport: '8080',hostname: 'www.baidu',hash: '#hash',search: '?id=1&name=tom',query: [Object: null prototype] { id: '1', name: 'tom' },pathname: '/images/fff/123/jj.jpg',path: '/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
parse方法
parse方法将url解析为对象,第一个参数是要解析成对象的url字符串,第二个是是否查询参数,为true,查询参数解析为对象,第三个是是否以//解析主机
const path = require("path");
const url=require("url");let str="/images/fff/123/jj.jpg";
console.log(path.parse(str));
结果:
{root: '/',dir: '/images/fff/123',base: 'jj.jpg',ext: '.jpg',name: 'jj'
}console.log(path.sep);// \
let u = "//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash";
console.log(url.parse(u));//query结果:
\
Url {protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: '#hash',search: '?id=1&name=tom',query: 'id=1&name=tom',pathname: '//www.baidu:8080/images/fff/123/jj.jpg',//pathname 属性是一个可读可写的字符串,可设置或返回当前 URL 的路径部分path: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u,true));Url {protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: '#hash',search: '?id=1&name=tom',query: [Object: null prototype] { id: '1', name: 'tom' },//第二个参数为true,query属性就会从查询字符串格式(“a=1&b=2”)转换为了对象格式({a: 1,b: 2})pathname: '//www.baidu:8080/images/fff/123/jj.jpg',path: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u, true, true));
Url {protocol: null,slashes: true,auth: null,host: 'www.baidu:8080',//hostport: '8080',hostname: 'www.baidu',hash: '#hash',search: '?id=1&name=tom',query: [Object: null prototype] { id: '1', name: 'tom' },pathname: '/images/fff/123/jj.jpg',path: '/images/fff/123/jj.jpg?id=1&name=tom',href: '//www.baidu:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}本文标签: parse方法
版权声明:本文标题:parse方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/IT/1694656896a254613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论