# Remove chars from response in variable

**URL:** <https://community.postman.com/t/remove-chars-from-response-in-variable/44818>\
**Category:** Help Hub\
**Created:** [March 6, 2023, 5:21pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818 "2023-03-06T17:21:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviation-explorer-57](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@aviation-explorer-57](https://community.postman.com/u/aviation-explorer-57)\
**Post date:** [March 6, 2023, 5:21pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818/1 "2023-03-06T17:21:15Z")

</div>

_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_:

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 7, 2023, 1:17pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818/2 "2023-03-07T13:17:10Z")

</div>

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

```auto
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](https://us1.discourse-cdn.com/getpostman/original/3X/d/6/d6ce3f79bada8fbc9cb6ca10d81a8a3824e94720.png)

---

<div class="post-metadata">

**Author:** ![aviation-explorer-57](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@aviation-explorer-57](https://community.postman.com/u/aviation-explorer-57)\
**Post date:** [March 7, 2023, 1:30pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818/3 "2023-03-07T13:30:16Z")

</div>

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 😂😂

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

Also is all this done in the test section

Huge thanks

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 7, 2023, 2:05pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818/4 "2023-03-07T14:05:03Z")

</div>

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](https://learning.postman.com/docs/sending-requests/variables/)

In particular.

[Using variables in scripts | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/#using-variables-in-scripts)

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

[Overview | Postman Learning Center](https://learning.postman.com/docs/introduction/overview/)

Including the Postman Training links under “Other Resources”.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/b/3b763d56d66445f4c34eeb66c64631335559a741.png)

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.

---

<div class="post-metadata">

**Author:** ![aviation-explorer-57](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@aviation-explorer-57](https://community.postman.com/u/aviation-explorer-57)\
**Post date:** [March 8, 2023, 4:20pm UTC](https://community.postman.com/t/remove-chars-from-response-in-variable/44818/5 "2023-03-08T16:20:33Z")

</div>

Hi this worked a charm thank you very much
