Tray Platform / Useful Scripts / Find multiple words/phrases

Find multiple words/phrases

  • On this page

The Text Helpers 'contains' operation is very useful for checking if a piece of text contains a keyword or phrase. It is however limited to only one word or phrase.

You can extend this by using the match javascript function which can use regex functionality.

An example scenario might be that we are looping through a list of results and each result has an array of 'attachments', with the first [0] attachment containing text. We might want to search through the text attached to each result.

We would do this by adding a script step and setting a variable such as 'text' which uses a jsonpath like $.steps.loop-1.value.attachments[0].text to pull in the text from the result.

This can then be used in the script box where we can add a list of keywords separated by |:

exports.step = function (input) {
const text = input.text;
const res = text.match(
/(csv|sheets|boolean|webhook|sso|loop|call workflow|callable|script|object|debug|text helper|data storage|list|http client)/gi
);
return res;
};

This will return results such as:

Or:

As you can see this script returns a list of all instances of the keywords found.

For the next step in your workflow you could either extract and count these keywords or, more likely, you could use a boolean connector to check that the result is not null: