# I need to change a character in msg.topic to something else

**URL:** <https://discourse.nodered.org/t/i-need-to-change-a-character-in-msg-topic-to-something-else/7319>\
**Category:** General\
**Created:** [27 January 2019 23:45 UTC](https://discourse.nodered.org/t/i-need-to-change-a-character-in-msg-topic-to-something-else/7319 "2019-01-27T23:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 January 2019 23:45 UTC](https://discourse.nodered.org/t/i-need-to-change-a-character-in-msg-topic-to-something-else/7319/1 "2019-01-27T23:45:23Z")

</div>

I have looked, but am confused, so am not confident to my options.

The problem as it is now is like this:  
I have data coming in from different places.  
The msg.topic determines "who" sent it.  
I used the topic because it leaves flexibility if/when I add more data sources.  
But I've sort of shot myself in the foot with this.

I have a path to where the log files are. Then I get the msg.topic and add it to the path and all is sweet. The msg.topic has a / in it.  
But I was wanting to have all files (as it turns out) not quite as per the topic's implied path.

So, the topic is "external/temperature"

I want to swap the / with a - so the file is "predetermined path" + "modified topic".

I am stuck on how to replace the / and replace it with - in a function node.

This is the idea.

```auto
var filename = "/home/pi/.node-red/public/databases/weather/";
var topic = msg.topic; // work needed here to replace the / with -
msg.filename = filename + topic + ".db";
var time = Date.now();
var x = msg.payload;
msg.payload = time + ' - ' + x;
return msg;

```

But I don't want the file stored in `/home/pi/.node-red/public/databases/weather/external/temperature`

More: `/home/pi/.node-red/public/databases/weather/external-temperature.dbd`

But I'm stuck how to.

I probably have been told, but I've forgotten.

I also think it is complicated because the / is a "special qualifier" and I can't remember how to get around that part too.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 January 2019 23:52 UTC](https://discourse.nodered.org/t/i-need-to-change-a-character-in-msg-topic-to-something-else/7319/2 "2019-01-27T23:52:07Z")

</div>

I nearly have it (typically) now.

For example:

```auto
var str = msg.topic;

var res = str.replace(/temp/, "-");

msg.topic = res;

return msg;

```

Given I inject:

```auto
[{"id":"ea68388a.cb83","type":"inject","z":"baf80d2f.6bd538","name":"","topic":"temp/external","payload":"test","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":3700,"wires":[["d6ba8a8.e508878"]]}]

```

I get:

```auto
{"_msgid":"14f57958.a65857","topic":"-/external","payload":"test"}

```

That is swapping the /temp/ with -.

Which proves the concept is doable. Just I am missing how to do it with the / rather than text.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 January 2019 23:55 UTC](https://discourse.nodered.org/t/i-need-to-change-a-character-in-msg-topic-to-something-else/7319/3 "2019-01-27T23:55:40Z")

</div>

SOLVED!

Replace:

```auto
var res = str.replace(/temp/, "-");

```

With

```auto
var res = str.replace("/", "-");

```

Sorry. Stupid me.
