# How to extract text from bulk text

**URL:** <https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881>\
**Category:** Help Hub\
**Created:** [June 12, 2019, 5:02pm UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881 "2019-06-12T17:02:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas\_g](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@thomas\_g](https://community.postman.com/u/thomas_g)\
**Post date:** [June 12, 2019, 5:02pm UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/1 "2019-06-12T17:02:23Z")

</div>

I need to get a value from a variable but the content can’t be parsed automatically (json conversion fails and searches fail too).

Indeed, this content is usually displayed using standard browser and executed on windows load (this sends back the variable to be used later).  
As Postman doesn’t generate this event, I don’t receive this variable and can’t extract it from the text.

In below an example, I need to extract data.access\_token value :

```
    <script type="text/javascript">

    /**
     * Check whether this page is loaded in an iframe or a popup.
     */
    function isInIframe() {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }

    /**
     * Create the message payload to send back to the parent window.
     */
    function buildMessageData() {
        var data = {};

        
            data.access_token = '1024_random_characters';
            data.token_type = 'Bearer';
            data.expires_in = '900';
            data.scope = 'openid\x20email\x20roles\x20tenant';
        
            data.state = 'state';
        

        return data;
    }

    /**
     * On window load, post the message payload back to the parent window.
     */
    function onWindowLoadHandler() {
        var parentWindow = window.isInIframe() ? window.parent : window.opener;
        
        parentWindow.postMessage(buildMessageData(), 'https\x3A\x2F\x2Fmyurl.com');
    }

    // Attach event handler for the 'DOM loaded' or equivalent event on the window
    if (window.addEventListener) {
        window.addEventListener('DOMContentLoaded', onWindowLoadHandler, false);
    } else if (window.attachEvent) {
        // IE8
        window.attachEvent('onload', onWindowLoadHandler);
    }
</script>
</head>
<body>

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [June 15, 2019, 6:46am UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/2 "2019-06-15T06:46:58Z")

</div>

How does your response look like? Could you provide a sample by using [https://www.mocky.io/](https://www.mocky.io/) and specifying what you want to extract?

---

<div class="post-metadata">

**Author:** ![thomas\_g](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@thomas\_g](https://community.postman.com/u/thomas_g)\
**Post date:** [June 17, 2019, 8:26am UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/3 "2019-06-17T08:26:50Z")

</div>

Hi,

My response looks like the following : [http://www.mocky.io/v2/5d074e5d3000009c54051f0f](http://www.mocky.io/v2/5d074e5d3000009c54051f0f)  
I need to extract this “random\_text\_1024\_Characters” in data.access\_token

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 17, 2019, 9:10am UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/4 "2019-06-17T09:10:35Z")

</div>

Hi @thomas_g,

not sure it´s the best solution, but you could use a regular expression:

```
var regEx = new RegExp("data.access_token = '(.*?)'");
var resultArray = regEx.exec(responseBody);
var access_token = resultArray[1];

```

Edit: I think what you´re looking for, is Javascript parsing in Postman. Doing a quick google search on that, I wasn´t succesful. Maybe you´re more lucky.  
Usually parsing with RegEx is quite brittle, and should not be the preferred solution.

Best,  
Christian

---

<div class="post-metadata">

**Author:** ![thomas\_g](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@thomas\_g](https://community.postman.com/u/thomas_g)\
**Post date:** [June 17, 2019, 10:12am UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/5 "2019-06-17T10:12:59Z")

</div>

Thanks a lot Christian, even if it’s not the preferred solution as you said it works 😃  
Do you know if I can use “sed” like command to modify a variable?

For instance my variable contains ‘\x2D’ instead of ‘-’

Thanks,

Thomas

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 17, 2019, 11:12am UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/6 "2019-06-17T11:12:06Z")

</div>

> [@thomas\_g](#):
>
> For instance my variable contains ‘\x2D’ instead of ‘-’

To me, this sounds more like an encoding problem, I´m not a Javascript expert, but I guess Javascript has commands to handle that.

But I guess replace() ([JavaScript String replace() Method](https://appdividend.com/2018/09/05/javascript-string-replace-example-tutorial/)) would also do the job.

Best,  
Christian

---

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [June 17, 2019, 12:41pm UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/7 "2019-06-17T12:41:26Z")

</div>

Your mocky URL doesn’t work for me 😕  
But I’d use replace() like @chrissbaumann mentioned, but that’s just my preference.

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 17, 2019, 1:02pm UTC](https://community.postman.com/t/how-to-extract-text-from-bulk-text/5881/8 "2019-06-17T13:02:07Z")

</div>

Didn´t work for me in Chrome either, but when I called it from Postman it did. 😉
