Need help with the 'Code' Node to take data from previous node and extract a specific set of numbers

Question:

I am trying to extract a piece of information from my email after a specific text from the output of the previous node. I am not able to get it working as desired.

Consider the following: The output of Node 1 is

16-04-2023 

Dear Customer,

Thank you for banking with us.

We wish to inform you that INR 12345.67 has been debited from your A/c no. 12345678 and blah blah blah...

Sometimes, the mail may move the INR value forward, so it might look like

16-04-2023 

Dear Customer,

INR 12345.67 has been debited from your A/c no. 12345678...

So, I tried writing a simple Javascript function to use on the ‘Code’ node to extract the numbers after the words ‘INR’ and give that as the output. But, the workflow is only returning an empty response. The code I tried using is:

const results = [];
for (const line of $input.all()) {
  const match = String(line).match(/INR\s*(\d+(\.\d+)?)/);
  const amount = match ? match[1] : null;
  if (amount) {
    results.push({
      json: {
        amount: amount
      }
    });
  }
}
return results;

My workflow

Share the output returned by the last node

The expected output is:
12345.67

Also, can anyone please suggest what option should I choose in ‘Mode’ for this workflow?

Information on your n8n setup

  • n8n version: 0.221.2
  • Database: SQLite
  • n8n EXECUTIONS_PROCESS: own
  • Running n8n via: Docker
  • Operating system: Ubuntu Server 22.04 LTS

Hi there,

So while using regex is probably the best way. You have two other options where you find the index of INR and after that the first index of has, and grab everything in between.
I like to use the split function though, as it is easy to understand and use.
So that would be something like textField.split(‘INR’)[1].split(‘has’)[0].trim()
You can also do this in a normal expression in a set node for example.

2 Likes

Hi @BramKn,

Thank you for the pointer, I was able to get this working.

Regards,
Jayavel S

1 Like

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