How to to get elements value from html response at <script> </script>

So I get a response something like this:

<!DOCTYPE html>
<html>
<head>
--------------------
</head>
<body>
    <script>
        function onClickButton(word) {
        if (word == 'yes'){
          var authorizationCode = '360ad5ce-ecfe-4ad4-83d1-9254e89a3ccc';
          var state = 'c8271b81-4229-6a1f-bf9c-758f11c1f5b1';
        } else {
          alert(word);
        }
      }
    </script>
    <div class="shb-psua-sign-app-sandbox-container">
    --------------------
    </div>
</body>
</html>

How do I get var authorizationCode value? and save it as a variable in postman.
My code for now looks like this:

const $ = cheerio.load(pm.response.text())
var script = $("script").text();

pm.collectionVariables.set("access_token", script);

Now I get the whole script but how do I just get the value of the authorizationCode or even just using the gotten variable to get the value out of it?
I have also looked about different solutions for example:

const script = $('script').find("authorizationCode");  //but this doesn't work

Hi @akfas

Not the cleanest way but you could try this;

const response = pm.response.text();
//find a match with a regex
let authCode = response.match(/authorizationCode = '(.*?)';/g);
//remove first and last characters as needed with slice;
console.log(authCode[0].slice(21, -2));

yeah, it works but I don’t know if the code is always that length

The length of code shouldn’t matter as the bits i’m removing are the start and end of the string…

So the slice 21 removes this;

authorizationCode = '

and the slice -2 removes this;

';

The bit you are left with is the code in between.

Like this;
image

So if I mock up the example with extra stuff in the code it looks like this;

Hope this helps…

yeah, sorry about that. I wasn’t fully concentrating when responding.

1 Like

no worries, glad it’s helped. :+1: