Hopefully a nice simple one: is it even possible to use .replace with a variable?

Describe the problem/error/question

I’m very early on on my javascript learning curve but look forward to the day I can help others as I do with other things I am technically proficent with.

Minimal, Reproducible Example:

Desired behaviour:

I am trying to use multiple .replace and currently have it working in a ‘Set’ node like:

{{$json["data"].replace('data1','output1').replace('data2','output2')}}

Specific problem or error:

What I can’t seem to get to work is when the .replace data I want to use is coming from a variable (as was generated earlier in the flow) instead of being typed manually.

Shortest code necessary to reproduce the problem:

Examples of some code that I have tried using different characters in-between like + and &, etc:

{{$json["data"]+$node["replaceList"].json["variable"]}} and
{{$json["data"]++$node["replaceList"].json["variable"]}} and
{{$json["data"]&$node["replaceList"].json["variable"]}} and
{{$json["data"]&&$node["replaceList"].json["variable"]}} and
{{$json["data"].$node["replaceList"].json["variable"]}} and
{{$json["data"]$node["replaceList"].json["variable"]}} etc.

(Where “variable” is: .replace('data1','output1').replace('data2','output2')

I would be deeply appreciative if anyone can spare a few seconds to suggest how my code should be formatted for it to work as all I get is [undifined].

What is the error message (if any)?

Usually just [undifined]

Please share your workflow

Although no idea if it’ll help, below is the code from that entire node:

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

n/a

Information on your n8n setup

  • n8n version: latest
  • Database (default: SQLite): ?
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ?
  • Running n8n via (Docker, npm, n8n cloud, desktop app): desktop, online
  • Operating system: windows, linux

Hi @binvius, by variable you mean a value from a node elsewhere in your workflow, right?

One thing to keep in mind here is that the order or number of items might differ between nodes. For example: For the fourth item on a random Set node somewhere in your workflow, n8n would try to look up the fourth item from your “source” node which might not exist.

n8n offers a number of variables and methods to account for this, and the right one would depend on your exact use case.

Here’s a simple example workflow with one “replacement list” Set node, a dummy node returning example items and finally a Set node performing an actual replacement using values from the first node:

The expression used here is {{ $json.country.replace($json.country, $('Replace list').first().json[$json.country]) }}. Let’s unpack what it does:

  • $json.country: This simply references the country field of each incoming item
  • .replace: This calls the actual .replace() method
  • ($json.country,: This simply references the current value of the country field again, assuming this is the value we want to replace.
  • $('Replace list').first().json[$json.country]): This is the interesting part. Here we’re referencing the first item from our Replace list node (so even for the second, third, fourth, etc. item on the current node we’d still look at the first and only item from the Replace list node). We are then looking up the field named after the current value of our country field.

This particular workflow would of course also work with a much shorter expression since we’re overwriting the entire field anyway in this example. But since you were asking about .replace() I hope it provides a bit of insight into how to use this method specifically in connection with data from elsewhere in your workflow :slight_smile:

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