//正则取url中参数,从?开始的都可以取,
const getUrlParam = (url, name) => {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(url);
if (!match) {
return "";
}
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
示例:url (https://baidu.com/index?id=123&name=lyl)
getUrlParam(url, "id")
getUrlParam(url, "name")
取出结果:
id: 123
name: lyl
正则取参代码块
网友评论