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

#1

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.

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.

#2

I nearly have it (typically) now.

For example:

var str = msg.topic;

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

msg.topic = res;

return msg;

Given I inject:

[{"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:

{"_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.

#3

SOLVED!

Replace:

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

With

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

Sorry. Stupid me.