Code JS to transform text with comma

Hello guys, I have a question!

I want a JavaScript code to be used in a “code” node of n8n, which, when inserting a text without a comma, returns itself. If the text has a comma, stop the words, and return the result with the words separated by comma and space after the comma. The last two words should be separated by “and” instead of comma. If there are only 2 words in the text, separate only by " and ".

Ex.:

Talita → Talita
Talita,John → Talita and John
Talita,John,Peter → Talita, Jonh and Peter

Could you please help me this issue?

Hey @leandrotimao,

You can do this with a bit of Regex which you can find in the workflow below, The good news is you can also do this in any field that accepts an expression which you can see in the Set node so you may not need the code node.

1 Like