JSONata in function node

In the process of learning JSONata I'm having difficulty figuring out how to utilize regex as a parameter. Take for example the following in a function node:

const example = "ababbabbcc";
// Applies the string to the pattern regular expression and returns an array of objects
const expression = jsonata(`$match(${example}, /a(b+)/)`);

msg.payload = expression.evaluate();
return msg;

I am under the impression this does not work, due to the regex being wrapped in backticks, thus becoming a string.

How can I go about achieving a result such as:

[
  {
    "match": "ab",
    "index": 0,
    "groups": ["b"]
  },
  {
    "match": "abb",
    "index": 2,
    "groups": ["bb"]
  },
  {
    "match": "abb",
    "index": 5,
    "groups": ["bb" ]
  }
]

As seen in the JSONata documentation here: String functions · JSONata

I wasn't aware that jsonata functions were available to use in a function node??

To clarify, I am using the external module jsonata - npm

I have not tested, but as example is a string it would have to have quotes around it, to form a correct expression. The expression is therefore looking for a property in an object, called example, but you did not feed it any data when you evaluated.

const expression = jsonata(`$match("${example}", /a(b+)/)`);

but probably better to do

let data = {
  example: "ababbabbcc"
}
const expression = jsonata('$match(example, /a(b+)/)');
msg.payload = expression.evaluate(data);
return msg;

Again untested.

1 Like

This worked, thank you for your help.

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