Replace characters

Hi all!

I need to replace the characters \n and \t from the String. I have a function but is not working properlly.

[
    {
        "id": "d9e4f025031e49e5",
        "type": "tab",
        "label": "prueba remplazo",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "2c626d2eb7245f69",
        "type": "inject",
        "z": "d9e4f025031e49e5",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Hello\\nCol\\t",
        "payloadType": "str",
        "x": 140,
        "y": 160,
        "wires": [
            [
                "9575e1e5f2f1aec1"
            ]
        ]
    },
    {
        "id": "9575e1e5f2f1aec1",
        "type": "function",
        "z": "d9e4f025031e49e5",
        "name": "",
        "func": "str = msg.payload;\n\nmsg.payload = str.replace(/\\n|\\t/g, \" \");\n\nreturn msg;\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 160,
        "wires": [
            [
                "8d794e7289334aa0"
            ]
        ]
    },
    {
        "id": "8d794e7289334aa0",
        "type": "debug",
        "z": "d9e4f025031e49e5",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 160,
        "wires": []
    }
]

Regards!

The Function code you have works.

The problem is you are not injecting a String value containing \n and \t control characters.

The Inject node doesn't support entering escaped literals like that - so the string you are injecting contains the literal \n and \t.

If you want to set a message property containing newlines or tabs, use the Template node.

Thanks a lot for the answer.
So, i added a Mustache template in order to manipulate the data but i think is not working properlly.

Any clues?.

[{"id":"a6a9eea3d3776f71","type":"function","z":"d9e4f025031e49e5","name":"","func":"\nmsg.payload.replace(/\\n/g,\" \");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":140,"wires":[["deabf7d14c6e749d"]]}]

The replace function returns the updated string - it doesn't modify the value in place.

So your Function code:


msg.payload.replace(/\n/g," ");

return msg;

should be:

msg.payload = msg.payload.replace(/\n/g," ");

return msg;

The other simple way to inject control characters is to set the inject to Json mode {} and send in a string (with quotes)
image

Uhm it doesn't work either.

I think maybe there is a problem (my fault) with the Inject node but i'm fooling around.

Any clues?.

Without seeing what you have in the Template node, it's impossible to say.

Here it is...

To add a newline in the template node, don't use \n - add an actual newline.

Uhm ok, the thing is that the message that i recieving comes exactly with those characters \n and \t that i need to replace with a " " (space).

Maybe is not the best example but i want to get an approach to that.

Regards,

Have you tried using the String node?

I've used it several times to operate with strings inside messages and it is quite powerful.

If that is the case, then the Function you had at the start would work. The issue was purely with how you are trying to simulate that data to test your flow.

Maybe i should convert my text in String format inside the function?

I mean, inside my function...

The Template node does what you need .. use it as Nick explained if you want to emulate the string that comes from your device.

the Debug window makes it more readable and easy for you to expand and view it if its a string with \n and tabs and may not show those characters.

If you want to see what special characters comes from your device .. one trick is to JSON.stringify it so you'll know what processing you need to do with command like .replace() .

msg.payload = JSON.stringify(msg.payload)
return msg;

I figured it out!

Javascript needs double slash to scape characters.
So the function goes like this.

var text = msg.payload;
msg.payload = text.replace(/\\n|\\r\\n|\\r/g, '');

return msg;

Thanks to all!
Regards!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.