# Replace characters

**URL:** <https://discourse.nodered.org/t/replace-characters/53770>\
**Category:** General\
**Created:** [15 November 2021 14:10 UTC](https://discourse.nodered.org/t/replace-characters/53770 "2021-11-15T14:10:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 14:10 UTC](https://discourse.nodered.org/t/replace-characters/53770/1 "2021-11-15T14:10:00Z")

</div>

Hi all!

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

```auto
[
    {
        "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!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 November 2021 14:18 UTC](https://discourse.nodered.org/t/replace-characters/53770/2 "2021-11-15T14:18:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 14:46 UTC](https://discourse.nodered.org/t/replace-characters/53770/3 "2021-11-15T14:46:22Z")

</div>

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?.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 November 2021 14:55 UTC](https://discourse.nodered.org/t/replace-characters/53770/4 "2021-11-15T14:55:49Z")

</div>

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

So your Function code:

```auto

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

return msg;

```

should be:

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

return msg;

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [15 November 2021 14:59 UTC](https://discourse.nodered.org/t/replace-characters/53770/5 "2021-11-15T14:59:30Z")

</div>

The other simple way to inject control characters is to set the inject to Json mode `{}` and send in a string (with quotes)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/12bea5eb1f013bf918bff28f62f0be79c9a44d58.png)

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 15:02 UTC](https://discourse.nodered.org/t/replace-characters/53770/6 "2021-11-15T15:02:29Z")

</div>

Uhm it doesn't work either.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f074b244effe6d23350ce30af43379ea46f77f3.png)

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

Any clues?.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 November 2021 15:03 UTC](https://discourse.nodered.org/t/replace-characters/53770/7 "2021-11-15T15:03:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 15:05 UTC](https://discourse.nodered.org/t/replace-characters/53770/8 "2021-11-15T15:05:10Z")

</div>

Here it is...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a907b7a095bbf522354e31e2c4f8dcef91d0ddd.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 November 2021 15:05 UTC](https://discourse.nodered.org/t/replace-characters/53770/9 "2021-11-15T15:05:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 15:08 UTC](https://discourse.nodered.org/t/replace-characters/53770/10 "2021-11-15T15:08:10Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [15 November 2021 15:20 UTC](https://discourse.nodered.org/t/replace-characters/53770/11 "2021-11-15T15:20:48Z")

</div>

Have you tried using the [String node](https://flows.nodered.org/node/node-red-contrib-string)?

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 November 2021 15:29 UTC](https://discourse.nodered.org/t/replace-characters/53770/12 "2021-11-15T15:29:33Z")

</div>

> [@Marty1982](#):
>
> 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).

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.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 15:56 UTC](https://discourse.nodered.org/t/replace-characters/53770/13 "2021-11-15T15:56:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 15:59 UTC](https://discourse.nodered.org/t/replace-characters/53770/14 "2021-11-15T15:59:34Z")

</div>

I mean, inside my function...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c051165d24a1e58a585a0338fbf7162a1e9a48fb.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 November 2021 16:10 UTC](https://discourse.nodered.org/t/replace-characters/53770/15 "2021-11-15T16:10:47Z")

</div>

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()` .

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

```

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 November 2021 17:19 UTC](https://discourse.nodered.org/t/replace-characters/53770/16 "2021-11-15T17:19:07Z")

</div>

I figured it out!

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

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

return msg;

```

Thanks to all!  
Regards!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 December 2021 18:25 UTC](https://discourse.nodered.org/t/replace-characters/53770/17 "2021-12-03T18:25:42Z")

</div>

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