[Solved] How to regex Zapier and get output?


David here, from the Zapier Platform team.

I’m glad you’re showing interest in the code step. Assuming your assumptions (32 characters exactly) is always going to be true, this should be fairly straightforward.

First off, the regex. We want to look for a character that’s a letter, number, or punctuation. Luckily, javascript’s \w is equivalent to [A-Z0-9a-z_], which covers the bases in all of your examples besides the -, which we’ll include manually. Finally, we want exactly 32 character length strings, so we’ll ask for that. We also want to add the global flag, so we find all matches, not just the first. So we have the following:

/[\w-]{32}/g

You’ve already covered mapping the body in, so that’s good. The javascript code will be as follows:

// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/[\w-]{32}/g) 

// the .map function executes the nameless inner function once for each 
// element of the array and returns a new array with the results
// [{str: 'loYm9vYzE6Z-aaj5lL_Og539wFer0KfD'}, ...]
return (matches || []).map(function (m) { return {str: m} })

Here, you’ll be taking advantage of an undocumented feature of code steps: when you return an array of objects, subsequent steps are executed once for each object. If you return an empty array (which is what’ll happen if no keys are found), the zap halts and nothing else happens. When you’re testing, there’ll be no indicator that anything besides the first result does anything. Once your zap is on and runs for real though, it’ll fan out as described here.

That’s all it takes! Hopefully that all makes sense. ​Let me know if you’ve got any other questions!

11

solved How to regex Zapier and get output?