获取URL参数

189 阅读1分钟
function getQueryVariable(url = '', name) {
    if (!url) {
        return null
    }
    var search = url.split('?')[1]
    if (!search) {
        return null
    }
    const reg = new RegExp(`(^|&)${name}=([^&]*)($|&)`, 'i')
    const res = search.match(reg)
    if (res === null) {
        return null
    }
    return res[2]
}