我正在尝试使用此函数在查询字符串中获取值
const getQueryStringValueByName = name => {
name.replace(/[\[\]]/g, "\\$&");
let url = window.location.href;
let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
它工作正常,但是当值具有&
时,它会像这样削减整个价值
ViewName=MyValue%20&%202ndString&FilteringColumn=OriginatingGroup
我所得到的只是
MyValue
我期望得到
MyValue & 2ndString