Formatting a string in nodered

Hi,

I’m struggling with the function node. I want to extract the volume number from the string and convert it to a number so I can control a volume slider on the UI.

image

I have very little knowledge of JavaScript and have cobbled together this code for a function node:

msg.payload = msg.payload.split("Volume:")[1]; return msg;

It kind of gets me to where I need to be but it seems clunky and I’m unable to convert to a number as the output is still delivered as an expression(?)

image

Any help would be greatly appreciated

const regex = /Volume:"(\d+)"/;
const volume = msg.payload.match(regex)[1];
msg.payload = Number(volume);
return msg;
2 Likes

…and just like that, you nailed it! I was just working on connecting a switch node to a CSV node to a template node and back to JSON node. I just got the result but your method is much neater.
Thank you very much!