url常用方法
parse
url.parse("https://www.baidu.com:8080/?sousuo=xjz")
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.baidu.com:8080',
port: '8080',
hostname: 'www.baidu.com',
hash: null,
search: '?sousuo=xjz',
query: 'sousuo=xjz',
pathname: '/',
path: '/?sousuo=xjz',
href: 'https://www.baidu.com:8080/?sousuo=xjz' }
url.parse("https://www.baidu.com:8080/?sousuo=xjz#floor",true)
//第二个参数为 true,query变为对象形式
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.baidu.com:8080',
port: '8080',
hostname: 'www.baidu.com',
hash: '#floor',
search: '?sousuo=xjz',
query: { sousuo: 'xjz' },
pathname: '/',
path: '/?sousuo=xjz',
href: 'https://www.baidu.com:8080/?sousuo=xjz#floor' }
url.parse("//www.baidu.com:8080/?sousuo=xjz#floor",true)
//没加第三个参数,解析的无协议的url
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#floor',
search: '?sousuo=xjz',
query: { sousuo: 'xjz' },
pathname: '//www.baidu.com:8080/',
path: '//www.baidu.com:8080/?sousuo=xjz',
href: '//www.baidu.com:8080/?sousuo=xjz#floor' }
url.parse("//www.baidu.com:8080/?sousuo=xjz#floor",true,true)
//加上第三个参数,解析没有协议的url
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.baidu.com:8080',
port: '8080',
hostname: 'www.baidu.com',
hash: '#floor',
search: '?sousuo=xjz',
query: { sousuo: 'xjz' },
pathname: '/',
path: '/?sousuo=xjz',
href: '//www.baidu.com:8080/?sousuo=xjz#floor' }
format
url.format({
... protocol: 'https:',
... slashes: true,
... auth: null,
... host: 'www.baidu.com:8080',
... port: '8080',
... hostname: 'www.baidu.com',
... hash: null,
... search: '?sousuo=xjz',
... query: 'sousuo=xjz',
... pathname: '/',
... path: '/?sousuo=xjz',
... href: 'https://www.baidu.com:8080/?sousuo=xjz' })
'https://www.baidu.com:8080/?sousuo=xjz'
resolve
url.resolve("https://www.baidu.com","/xjz")
'https://www.baidu.com/xjz'
querystring常用方法
stringify
querystring.stringify({name:13} )
'name=13'
querystring.stringify({name:13,day:222},',' )
'name=13,day=222'
querystring.stringify({name:13,day:222},',',':')
'name:13,day:222'
parse
querystring.parse('name=13')
{ name: '13'}
querystring.parse('name=13,day=222')
{ name: '13,day=222'}
querystring.parse('name=13,day=222',',')
{ name: '13', day: '222' }
querystring.parse('name:13,day:222',',',':')
{ name: '13', day: '222'}
escape和unescape
querystring.escape('哈哈哈')
'%E5%93%88%E5%93%88%E5%93%88’
querystring.unescape('%E5%93%88%E5%93%88%E5%93%88')
'哈哈哈’
网友评论