导入系统模块:const url = require('url');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const url = require('url')
let str = 'http://www.baidu.com/abc/qqq?flag=123&keword=javascript'
//parse方法将字符串转换成对象
let ret = url.parse(str, true)
console.log(ret)
//输出结果:
/*Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.baidu.com',
port: null,
hostname: 'www.baidu.com',
hash: null,
search: '?flag=123&keword=javascript',
query:
[Object: null prototype] { flag: '123', keword: 'javascript' },
pathname: '/abc/qqq',
path: '/abc/qqq?flag=123&keword=javascript',
href: 'http://www.baidu.com/abc/qqq?flag=123&keword=javascript'
}*/