Javascript 从窗口位置获取值。网址
我在window.location.href中有响应url,我需要它的Javascript 从窗口位置获取值。网址,javascript,regex,Javascript,Regex,我在window.location.href中有响应url,我需要它的错误、错误描述和状态的值 我正在使用以下代码,但得到的是空值 var messageType = new RegExp('[\?&]' + "error" + '=([^&#]*)').exec(window.location.href); 我需要从url“用户忘记了他们的密码”中找到这个字符串。这里的问题是,您的url参数前面有一个,而不是? 因此,只需更换它并使用以下方法访问参数: var pre
错误
、错误描述
和状态
的值
我正在使用以下代码,但得到的是空值
var messageType = new RegExp('[\?&]' + "error" + '=([^&#]*)').exec(window.location.href);
我需要从url“用户忘记了他们的密码”中找到这个字符串。这里的问题是,您的url参数前面有一个
,而不是?
因此,只需更换它并使用以下方法访问参数:
var prevUrl=”http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a++用户+已经+忘记了+他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3”;
var url=新url(prevUrl.replace(/#/,“?”);
log(url.searchParams.get(“错误”);
log(url.searchParams.get(“错误描述”)
这里的问题是URL参数前面有一个
,而不是?
因此,只需更换它并使用以下方法访问参数:
var prevUrl=”http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a++用户+已经+忘记了+他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3”;
var url=新url(prevUrl.replace(/#/,“?”);
log(url.searchParams.get(“错误”);
log(url.searchParams.get(“错误描述”)代码>在(现代)浏览器中,您可以使用新URL()
轻松解析URL和提取查询参数
var location_url="http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3";
//To make it work you have to replace "/#" with '?' so that new URL() constructor parses the url properly.
location_url=location_url.replace('\/#','?');
var url = new URL(location_url);
var error = url.searchParams.get("error");
console.log(error);
在(现代)浏览器中,您可以使用新URL()
轻松解析URL和提取查询参数
var location_url="http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3";
//To make it work you have to replace "/#" with '?' so that new URL() constructor parses the url properly.
location_url=location_url.replace('\/#','?');
var url = new URL(location_url);
var error = url.searchParams.get("error");
console.log(error);
您可以按&
拆分项目,然后按=
拆分项目
//您将做什么:
//const hash=location.hash;
//出于演示目的:
常量散列='#错误=访问被拒绝&错误描述=AADB2C90118%3a+用户+忘记了+他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-259C641b'';
const hashParams=hash.substr(1).split(“&”)
.reduce((obj,groupStr)=>
对象分配(obj{
[groupStr.split('=')[0]]:groupStr.split('=')[1]
}), {});
console.log(hashParams);
console.log(hashParams.error_description)代码>您可以按&
拆分项目,然后按=
拆分项目
//您将做什么:
//const hash=location.hash;
//出于演示目的:
常量散列='#错误=访问被拒绝&错误描述=AADB2C90118%3a+用户+忘记了+他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-259C641b'';
const hashParams=hash.substr(1).split(“&”)
.reduce((obj,groupStr)=>
对象分配(obj{
[groupStr.split('=')[0]]:groupStr.split('=')[1]
}), {});
console.log(hashParams);
console.log(hashParams.error_description)代码>像这样使用注册表:
let url='http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3';
let result=url.match(/error_description=([\s\S]*?)\./)[1].split('+');
result.shift();
console.log(result.join(' '));
像这样使用注册表:
let url='http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3';
let result=url.match(/error_description=([\s\S]*?)\./)[1].split('+');
result.shift();
console.log(result.join(' '));
如果您仍然希望使用RegExp,即使使用前面答案中的新选项可能会有点过头,您可以使用如下RegExp:
let url='http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3';
let result=url.match(/error_description=([\s\S]*?)\./)[1].split('+');
result.shift();
console.log(result.join(' '));
const regex=/(error | error_description)=(.+?)和/g;
常量str=`http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a++用户+忘记了他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3`;
让火柴;
while((matches=regex.exec(str))!==null){
if(matches.index==regex.lastIndex){
regex.lastIndex++;
}
matches.forEach((match,groupIndex)=>{
if(groupIndex==1)
log(`Param:${match}`);
if(groupIndex==2)
log(`Value:${match}`);
});
}
如果您仍然希望使用RegExp,即使使用前面答案中的新选项可能会有点过头,您可以使用如下RegExp:
let url='http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a+The+user+has+forgotten+their+password.%0d%0aCorrelation+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin_hint%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3';
let result=url.match(/error_description=([\s\S]*?)\./)[1].split('+');
result.shift();
console.log(result.join(' '));
const regex=/(error | error_description)=(.+?)和/g;
常量str=`http://localhost:4200/#error=access_denied&error_description=AADB2C90118%3a++用户+忘记了他们的+密码。%0d%0a相关+ID%3a+a7916eb9-4404-4cc5-b5a3-ee3211237566%0d%0d%0aTimestamp%3a+2018-02-05+09%3a19%3a07Z%0d%0alogin\u提示%3a+kzahid%40opm.co.uk%0d%0a&state=da2d6e3c-cb6f-1d3b-909b-c6412325b3`;
让火柴;
while((matches=regex.exec(str))!==null){
if(matches.index==regex.lastIndex){
regex.lastIndex++;
}
matches.forEach((match,groupIndex)=>{
if(groupIndex==1)
log(`Param:${match}`);
if(groupIndex==2)
log(`Value:${match}`);
});
}