How to exclude string in response body

Hi,

I am struggling with some issue regarding data in response body.
In my response body i get “body”: “Please go to https://admin.dev.foo.com to reset your password”.
I then create a set environmnet variable to transer “Please go to https://admin.dev.foo.com to reset your password”. The issue is that I want to exclude “Please go to” and “to reset your password” and only include the url -"https://admin.dev.foo.com ".
Is there a solution to exculde some strings/text ?

Hey @Calibraticboy,

You could use Regex and try something like this:

let jsonData = pm.response.json().message.body

var url = jsonData.match(/(https?:\/\/[^ ]*)/)[1];
    
pm.globals.set('url', url) 

I’ve only tested this out locally with the string “Please go to https://admin.dev.foo.com to reset your password” and it seemed to extract and save the URL.

I’m absolutely not a regex expert :slight_smile:

1 Like

Thanks for your help :slight_smile:I will give it a try.

Like Danny, I’m absolutely not a regex expert - But here’s how I’d personally tackle your problem:

Let’s say the below response body is returned:

{
    "subject": "Please go to https://gear4music.com to reset your password"
};

My tests tab would have the below to capture that string, and “replace” all the unwanted text with empty values.

image

So you’re environment variable will be:

This is the cool thing about writing code/tests - the same problem can have multiple “solutions” depending on the user’s preference :slight_smile:

1 Like

Looks interesting :slight_smile:
I will give it a try. Thanks for helping.

Works like a charm :slight_smile:

1 Like

Awesome! :trophy:

Which method did you go for in the end?

Both method worked, but I ended working with pfarell solution becuase I am not familiar wirh regex.

2 Likes