K.Z
K.Z

Reputation: 5075

Get values from window location . url

I have response url in window.location.href and I need the value for error, error_description and state from it

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

I am using following code but getting null value

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

I need find this string from url "The user has forgotten their password"

Upvotes: 1

Views: 373

Answers (5)

Dez
Dez

Reputation: 5838

If you still want to use a RegExp even though might be an overkill having the new options from the previous answers, you can use a RegExp like this:

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

Here you have 2 capture groups inside the full match, so you can get separatedly the parameter name and its value.

(error|error_description) -> will match either error or error_description

(.+?) -> will match from 1 to any characters until it finds the next character match, stated in this case by &, as few times as possible and expanding as needed

The g (global modifier) will allow to return all the matches found.

Upvotes: 1

xianshenglu
xianshenglu

Reputation: 5369

use reg like this:

    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(' '));

Upvotes: 1

nanobar
nanobar

Reputation: 66435

You could split by & and then split items by =.

// 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);

Upvotes: 1

Mohd Asim Suhail
Mohd Asim Suhail

Reputation: 2292

In a (modern) browser you can use the new URL() to parse your url and extract query parameters easily.

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);

Upvotes: 1

Zenoo
Zenoo

Reputation: 12880

Your problem here is that your URL parameters are preceded by a #, not a ?.

So, simply replace it and access the parameters using 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"));

Upvotes: 2

Related Questions