Error using regex groups in expression - How to return an object from a Regular expression match using named capture groups?

Describe the problem/error/question

I’d like to be able to use an expression in a Set node to return an object of regular expression named capture groups, as an output object that can then be accessed by key/value in downstream nodes. The expression itself seems to return the correct object in the expression preview, but when executing the node fails in a somewhat unclear manner.

I’ve attached a sample workflow that parses the following string:

Color: Red, Size: Large

… in to two capture groups, with the following regular expression (expression):

{{ /Color: (?<color>.*), Size: (?<size>.*)/.exec($json.input_data).groups }}

… which should result in an object like the following (and this is what the expression preview shows):

[Object: {"color": "Red", "size": "Large"}]

What is the error message (if any)?

ERROR: Cannot read properties of undefined (reading 'name')

Please share your workflow

Share the output returned by the last node

Error details/cause:

ERROR: Cannot read properties of undefined (reading 'name')

[{"name":"match_groups","value":"={{ /Color: (?<color>.*), Size: (?<size>.*)/.exec($json.input_data).groups }}"}]

Error stack trace:

Stack

TypeError: Cannot read properties of undefined (reading 'name')
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1212:96
    at Array.forEach (<anonymous>)
    at cleanupParameterData (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1209:32)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1205:38
    at Array.forEach (<anonymous>)
    at cleanupParameterData (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1205:19)
    at getNodeParameter (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1293:9)
    at Object.getNodeParameter (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/NodeExecuteFunctions.js:1519:24)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Set/Set.node.js:173:18)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:649:42)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:631:68

Information on your n8n setup

  • n8n version: 1.1.1
  • Database: SQLite
  • n8n EXECUTIONS_PROCESS setting: main? Not specified/default
  • Running n8n via: docker (compose)
  • Operating system: Ubuntu 22.04.3

Thanks!

Hi @craSH :wave: Welcome to the community :tada:

After some testing (and shaking off some Regex rust :joy: ) I managed to get this working as an array, would this work for you?

Someone else may have a less messy solution :see_no_evil:

Thanks! Yeah, unfortunately that will get really messy with the regex that I actually have over here to work with (about a dozen capture groups, and much more complex logic than the sample here). You may also have multiple matches from a single regex evaluation, so having each match returned as its own object is important since you could for example have dozens of different groups (e.g. “color” matches in this example) for a single evaluation.

I’m not a JS dev, so I may have been a bit misguided in thinking .groups should be available. That seems to be a feature that was added in ES2018, maybe the Node engine used for the code node doesn’t support it? I found some background here: regex - Javascript: Named Capture Groups - Stack Overflow

Alternatively, that same page mentions a more robust JS regular expression library XRegExp which would provide this and some other advanced regex features.

It’d be awesome to be able to do this in an expression, but it’s also not too bad to do in a Code node (the introduction of Python makes this even easier with their re module)

That’s totally fair :see_no_evil:

I’m not too sure if it helps, but you can find a list of our built-in variables and methods here, which detail what’s available in the Code node: Built in methods and variables reference | n8n Docs

1 Like

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