1. parseUrl

let path = 'http://test.sh.cn/tms/order?name=zs&age=18&id=1'
const parseUrl = (url) => {
    const protocol = url.substr(0, url.indexOf(':'))
    let sub = url.substr(protocol.length + 3)
    const domain = sub.substr(0, sub.indexOf('/'))
    sub = sub.slice(domain.length)
    const path = sub.substr(0, sub.indexOf('?'))
    sub = sub.slice(path.length + 1)
    console.log(sub)
    
    const query = sub.split('&').reduce((pre, cur) => {
        let k = cur.split('=')[0]
        let v = cur.split('=')[1] || undefined
        pre[k] = v
        return pre
    }, {})
    
    
    return {
        protocol, domain, path, query
    }
    
}

parseUrl(path)

 

posted @ 2022-08-29 17:35  monkey-K  阅读(15)  评论(0编辑  收藏  举报