通过正则获取地址栏参数

95 阅读1分钟
export function getUrlKey(name){
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null 
}

例子: https://www.baidu.com/?token=ceshi000

getUrlKey('token')结果就是 'ceshi000'