Remove ascii characters from payload

These do not seem to work as it complains that *.replace is not a function.

//var msg = {payload:msg.payload.replace(/[^\x00-\x7F]/g, “”)};
//msg.payload = msg.payload.Replace(/[^\x00-\x7F]/g, “”);

//var msg = {payload:msg.payload.replace(/[^\x00-\x7F]/g, “”)};
//return msg;

Can someone send me the code on how to parse out characters from a payload? I have been beating my head against a wall on this for about 4 hrs, and there is limited documentation on how to properly use the ‘replace’ within a function.

Thanks!!

Alphdev

Hi @alphadev

If, when you use msg.payload.replace(...) , it says replace is not a function, that means msg.payload is not a String type.

So the question is what type is it? If you wire your message to a Debug node it will help you identify what it is - without knowing that, its hard to suggest what to do next.

Nick

in one of your lines you have a capital ‘R’ in replace while the other lines have a lowercase ‘r’…

In addition all the lines are commented out.

My method of asking for help is to create a small example: inject -> function-> debug showing the issue. The inject should send in the data you are processing so someone can try to reproduce the issue easily and maybe give you sme help…

it was easier than I had thought. I just had to get my head in the game…

I dragged over a ‘change’ node from the left side, then “set msg.payload” to javascript expression: payload.$replace (“°”,"")

That pulled off all the degree symbols off of my web query. I get what you are saying about passing the msg.payload in the return statement rather than just ‘msg’. - rookie mistake.

Thanks for your help! much appreciated. Nice to have an active contributing group! :slight_smile:

A bit late but the information is current for those who want to clear the payload message.
if you are going to use the replace method to remove characters from msg.payload it should be doing so:
ex:
var variable = msg.payload.replace ("\ n", ""); // this replaces the n character with the null character, in which case it only removes the \ n character.
To make a substitution it can even be done like this:
var variable = msg.payload.replace ("\ n", ":"); // in this case replaces the character \ n with the character ':'. In the case of our friend alphadev he could perform this procedure several times to eliminate all msg.payload characters. I hope that even late I have helped.

I post this because it worked correctly for me using the initial thinking of alphadev.