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


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.


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:


I get:


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.




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


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

Sorry. Stupid me.