从窗口位置获取值。网址

时间:2018-02-05 10:17:28

标签: javascript regex

我在window.location.href中有响应网址,我需要errorerror_descriptionstate的值

  

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

我正在使用以下代码,但获取空值

  var messageType = new RegExp('[\?&]' + "error" + '=([^&#]*)').exec(window.location.href);

我需要从网址“用户忘记密码”中找到此字符串

5 个答案:

答案 0 :(得分:2)

您的问题是,您的网址参数前面有#,而不是?

因此,只需替换它并使用URLSearchParams#get()

访问参数



var prevUrl = "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";
var url = new URL(prevUrl.replace(/#/,'?'));

console.log(url.searchParams.get("error"));
console.log(url.searchParams.get("error_description"));




答案 1 :(得分:1)

在(现代)浏览器中,您可以使用new 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);

答案 2 :(得分:1)

您可以按&拆分,然后按=拆分项目。

// what you would do:
//const hash = location.hash;

// for demo purposes:
const hash = '#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';

const hashParams = hash.substr(1).split('&')
  .reduce((obj, groupStr) =>
    Object.assign(obj, {
      [groupStr.split('=')[0]]: groupStr.split('=')[1]
    }), {});
  
console.log(hashParams);
console.log(hashParams.error_description);

答案 3 :(得分:1)

像这样使用reg:

fromLocaleString

答案 4 :(得分:1)

如果您仍然想使用RegExp,即使可能是一个过度使用前一个答案的新选项,您也可以使用这样的RegExp:



const regex = /(error|error_description)=(.+?)&/g;
const str = `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 matches;
while ((matches = regex.exec(str)) !== null) {
    if (matches.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    matches.forEach((match, groupIndex) => {
      if (groupIndex === 1 )
        console.log(`Param: ${match}`);
       if (groupIndex === 2 )
        console.log(`Value: ${match}`);
    });
}




/(error|error_description)=(.+?)&/g

在完全匹配中有2个捕获组,因此您可以分别获取参数名称及其值。

(error|error_description) - >将匹配错误或error_description

(.+?) - >将匹配从1到任何字符,直到找到下一个字符匹配,在本例中由&表示,尽可能少,并根据需要进行扩展

g(全局修饰符)将允许返回找到的所有匹配项。