Javascript 如何在jQuery中获取两个字符串之间url的特定部分
我正在尝试从我拥有的url中提取特定ID 我需要的ID是=4807307 它始终具有字符串PolicyId=before和&endorsementtid=afterJavascript 如何在jQuery中获取两个字符串之间url的特定部分,javascript,jquery,Javascript,Jquery,我正在尝试从我拥有的url中提取特定ID 我需要的ID是=4807307 它始终具有字符串PolicyId=before和&endorsementtid=after 如何从url中提取。使用拆分在=上拆分,然后在&上拆分以获取值 var class='1〕https://myhost.com/ReferredSummary.aspx?PolicyId=4807307&EndorsementId=5941939&EditExisting=true&NewClient=true&Adjustme
如何从url中提取。使用拆分在
=
上拆分,然后在&
上拆分以获取值
var class='1〕https://myhost.com/ReferredSummary.aspx?PolicyId=4807307&EndorsementId=5941939&EditExisting=true&NewClient=true&Adjustment=True';
console.log(a.split('=')[1].split('&')[0])
像这样的通用函数应该能够获取任何参数
function getUrlParameter(parameterName) {
return new RegExp(parameterName + "=([^&]+)", "i").exec(document.URL)[1];
}
因此,像geturlparmeter(“policyid”)
这样的调用应该可以做到这一点
这是当前不区分大小写的,如果希望参数与参数完全匹配,请使用returnnewregexp(parameterName+“=([^&]+)”).exec(document.URL)[1]
以下是您可以测试的代码片段:
var testUrl=”https://myhost.com/ReferredSummary.aspx?PolicyId=4807307&EndorsementId=5941939&EditExisting=true&NewClient=true&Adjustment=True";
var selectElement=document.querySelector(#select”),
resultElement=document.querySelector(#result”);
//添加要选择的参数
testUrl.substring(testUrl.indexOf(“?”)+1).split(“&”).forEach(函数(param){
var newOption=document.createElement(“选项”);
newOption.textContent=newOption.value=param.substring(0,param.indexOf(“=”);
选择Element.appendChild(新建选项);
});
//添加要选择的侦听器
selectElement.addEventListener(“输入”,updateResult);
updateResult();
函数updateResult(){
resultElement.textContent=getUrlParameter(selectElement.selectedOptions[0]。值);
}
函数getUrlParameter(parameterName){
返回新的RegExp(parameterName+“=([^&]+),“i”).exec(testUrl)[1];
}
那么您想要PolicyId???也是重复的:consturlparms=newurlsearchparms(window.location.search);const myParam=urlParams.get('PolicyId')代码>已回复100份准备回复100份