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 |:

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

This will return results such as:

script-contains-multiple-keywords

Or:

script-contains-multiple-keywords-second-result

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:

script-contains-multiple-keywords-boolean