How to remove whitespace in a returned item

Hi All :slight_smile:

Wonder if someone could help me with what is probably a easy question. I am using Cherrio to get an array of items from a website. The data returned looks like the following in Console:

image

I was wondering if there is a way to remove all the white space?

pm.test("Example", function(){

    const $ = cheerio.load(pm.response.text())

    let getAllInstancesOfData = $("td[class='dataDark']").toArray();

    
    let mydata= getAllInstancesOfData[1].children[0].data;

console.log(mydata);
});

Simplest way would just to replace spaces with nothing.

myData = myData.replace(' ', '');
1 Like

Hey @postnewbie2020

Maybe something like?

myData.replace(/\s+/g, '');
1 Like

Yeah, this is better than mine.

1 Like

Different…not better :smiley:

1 Like

mydata.trim()

Why not this ? :fearful: am i missing something

@praveendvd - i tried that before submitting my question, and it did not remove the whitespace. Thanks though :slight_smile:

Thats interesting is this an open api that we can try out ? did other solution works ?

Hi @danny-dainton, thanks for the suggestion. I have implemented this. However, I still get the same thing logged out in the console.

Could it be that what the console is showing, is in fact not white space? This is what I have off the assertion I am doing:

AssertionError: expected ‘\n\t\t\t\t\t\t9.34\n\t\t\t\t\t’ to deeply equal ‘9.34’

@praveendvd I guess it all depends what the structure of the string is - trim() only removes whitespace from the beginning and the end of a string.

For example:

@postnewbie2020 It looks like you have a mixture of whitespace and different carriage returns or newlines. From the image, you have provided it’s difficult to know what’s going on.

Can you provide an example of the HTML page it’s looking at so it will be easier to run locally rather than taking a guess :smiley:

1 Like
const regex = /\t|\n/ig;
console.log(myData.replaceAll(regex, ''));

trim removes only \n and \r , not \t , \t is tab