Trouble with regex in Zapier for extracting URL

Update: Issue solved. The problem was in the output variable declaration.

I’m trying to pull a URL from a VSTS attachment to sync with Jira. I set up a Run Javascript block in Zapier with this input:

comment: 
creationDate: 2017-10-11T11:31:19.293Z
lastModifiedDate: 2017-08-29T12:32:51.393Z
location: https://project.visualstudio.com/_apis/wit/attachments/ca6206de-0fab-451a-b0bc-89e70221dfcb
name: Capture.PNG
resourceId: 2255679

I want to grab the ‘location’ bit. My code looks like this:

let urlPattern = new RegExp('location: (.*)');
let result = urlPattern.exec(inputData);
let output = [{'url': result[1], 'extra': 'info'}];

But I keep getting this error:

TypeError: Cannot read property '0' of null

It works fine when I test it outside Zapier. Any ideas what’s going wrong here?

dude, ur regex might b too simple for multi-line stuff. try sumthin like this:

let urlPattern = /location:\s*(https?://\S+)/i;
let result = urlPattern.exec(inputData.comment);
if (result) {
output = [{url: result[1], extra: ‘info’}];
} else {
output = [{error: ‘no url found’}];
}

this should handle newlines n spaces better. lmk if it works!

It seems the issue might be with how Zapier handles multi-line input. Try modifying your regex to account for possible newlines and spaces:

let urlPattern = /location:\s*(https?:\/\/[^\s\n]+)/i;
let result = urlPattern.exec(inputData.comment);
if (result && result[1]) {
    output = [{'url': result[1], 'extra': 'info'}];
} else {
    output = [{'error': 'URL not found'}];
}

This approach uses a more specific regex to target the URL directly, and includes error handling. Also, make sure you’re accessing the correct input field (inputData.comment in this case). If the problem persists, double-check how Zapier is formatting the input data, as it might differ from what you’re expecting.

Hey there, I’ve run into similar issues with Zapier before. One thing that often trips people up is how Zapier handles input data. Have you checked if the input is actually coming in as a string? Sometimes Zapier can pass objects instead of raw text.

Try logging the inputData to see its structure:

console.log(JSON.stringify(inputData, null, 2));

If it’s an object, you might need to access the comment field specifically:

let commentText = inputData.comment || inputData.body || JSON.stringify(inputData);
let urlPattern = /location:\s*(https?:\/\/\S+)/i;
let result = urlPattern.exec(commentText);

This approach has saved me countless headaches. Also, don’t forget to handle cases where the regex doesn’t find a match – it’ll prevent those pesky null errors. Hope this helps!