Removing 'New Line' from a python script

I've got a python script running the input for a 16 key matrix keypad. I've got a node that runs the script and outputs the keypad's inputs. After every keystroke, it adds a line break or something that I don't know enough about programming to remove from the code itself.
I was hoping there was a way to filter that line break (or whatever it's called) out in node red and just leave the character.


I've figured out an extremely round-about way that works, but is laughably inefficient, involving a split and a switch with 16 outputs; just not pleasant to work with.
Thanks for the time.

use the string.replace() method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace in a function node with this regex:
https://regex101.com/r/dUyoA9/1
something like:

msg.payload = msg.payload.replace(/[\n\r]+/g,““);
return msg;

not tested as im on my phone
Johannes

I think trim() will do it too.
msg.payload = msg.payload.trim();
return msg

2 Likes

never tried trim with a carriage return, would be good to know if it works.

Trim did the trick! Thanks for the help, guys. Man, you all are fast on the response here. You all need to get out, hang out with friends in publ......oh yeah.....

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.