Search code examples
node-red

node-red split, reorder and join an output string


I am trying to reorder an output string in node-red like the following example:

Output:

msg.payload: rgb(152,11,100)

I want to have it like this:

mg.payload: rgb(11,100,152)

I tried with split node using , string, then I got three outputs, but I could not join them again with the order I want


Solution

  • The easiest way to do this is probably just with a regex in a function node.

    var re = /rgb\((\d+),(\d+),(\d+)\)/;
    var match = re.exec(msg.payload);
    msg.payload= "rbg(" + match[2] + "," + match[3] + "," + match[1] + ")";
    return msg