Remove chars from response in variable

My question:

I need to remove / and - from a response

Details (like screenshots):

I have the response and it’s 10/20-1 and the variable is called mId

I tried stringify and it puts the variable in quotes… can anyone help with the command to remove the chars??

I’m daisy chaining a lot of apis together so this is key thanks

How I found the problem:

I’ve already tried:

You should be able to do this with a regular expression.

let string = "10/20-1";

let newString1 = string.replace(/[^0-9.]+/g,"");
let newString2 = string.replace(/[\/\-,\s]+/g, "");
let newNumber = parseInt(string.replace(/[\/\-,\s]+/g, ""));

console.log(newString1);
console.log(newString2);
console.log(newNumber);

The first variant just removes any characters not on the whitelist.

The second variant removes just the forward slash and the hyphen. Both need to be escaped.

If you want it to be a number, then you will need to parse it.

Console Log

image

That’s great but that numeric value is in a variable field so how do I do it from there? In the Example you have the value almost as a constant definition

Sorry I’m learning new things I’ve been on mainframe for the last 25 years so a bit behind :joy::joy:

Do I just replace the constant with the {{variable}}

Also is all this done in the test section

Huge thanks

I don’t have access to your response, so I have no choice but to declare it as a variable to provide you with example code. You need to replace the string with your response variable.

Please review the following page.

Using variables | Postman Learning Center

In particular.

Using variables in scripts | Postman Learning Center

If you are new to Postman, then I would suggest a visit to the Learning Centre.

Overview | Postman Learning Center

Including the Postman Training links under “Other Resources”.

image

I would recommend the “Galaxy APIs 101” course first as it gets you used to the application features.

Then the “Galaxy Testing and Automation” course which teaches you how to assert the responses. It includes defining and using variables from responses.

1 Like

Hi this worked a charm thank you very much