Regex .replace() doesn't store in variable

Describe the issue/error/question

I am using Regex .replaceAll() to alter a string, but the result of one of the replace functions isn’t stored in the variable.

Being: message = message.replaceAll(/\n/g, " ");

The weird thing is that when I declare the variable inside of the code node, it does work. But it doesn’t when I use the input from the node.

I tried the exact same code in an online javascript compiler and there it did work.

What am I doing wrong?

What is the error message (if any)?


Please share the workflow

Below you find the two outcomes. The one with set uses the input of the node. And the other code node declares the string inside.

Share the output returned by the last node

"Hi Mark,\n\nI will chat with the guys and we will get those booked.\n\nKind regards\n\nxxxx \nmobile: 00000000000\nemail: [email protected]\nweb:\n\n[signature_2171257750]\n\n",

Information on your n8n setup

  • n8n version: 0.219.1
  • Running n8n via: Docker

Hey @LinkedUp_Online,

It looks like you need to escape backslash in your regexp. Thi is probably due to technical reasons regarding how n8n processes strings in provided code snippets.
Following code worked for me (pay attention to line 6):

var message = $input.item.json.message

var type = typeof(message);

message = message.split("From: ")[0]
message = message.replaceAll(/\\n/g, "$");
message = message.replaceAll(/\<(.*?)\>/g, "");

// Log to console

return {json:{message: message, type: type}}


1 Like

Hi Serg,

Thanks! I will try your solution :slight_smile:


1 Like

@serg it did fix the problem, thanks! But wouldn’t this be a bug that needs fixing?

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