Formatting of variable insertion causing issues with custom JS

I’m using a regex extraction to get a URL from a string, but when I try to insert a variable from WeWeb as a variable into my JS code, the formatting seems to be messing it up. Has anyone else run into this?

Here is the code, an example variable would be any string in the format “location: https://…”

function extractURLFromString(text) {
  const regex = /\location:\s*(https?:\/\/\S+)/i;
  const matches = text.match(regex);

  if (matches && matches.length > 1) {
    return matches[1];
  } else {
    return "URL not found.";
  }
}

const text = "variables['387438e2-280c-4878-89ba-bf09ca742c6a']";
const qbo_auth_url = extractURLFromString(text);

return(qbo_auth_url)

And this is returning “URL not found”.

remove the quotes around the variable for text, otherwise it’s a string literal, not a reference to the variable:

text ="variables[asdf]" // this is just a string literal
text = variables[asdf] // this will be weweb's variable
1 Like

Thank you. Still not working though (returning URL not found), perhaps because there are quotations in the string literal variable that I’m inserting?

what is the current value of the variable? according to your code it’s not passing the test, so you need to check what’s its current value.

1 Like

Right, the current value is:

> "location: https://appcenter.intuit.com/app/connect/oauth2?client_id=XXXXXX&scope=com.intuit.quickbooks.accounting&redirect_uri=https%3A%2F%2Fapp.ZZZ.ZZZ%2Fqbo&response_type=code&state=test1"

When I set this to the text variable directly (instead of passing through the WeWeb syntax representing this value), it works properly (the URL is extracted and the value is returned).

there is no such thing as weweb syntax. It’s just javascript. In the context of the function there is an object variables with the values of the variables created in weweb.
If it is returning an unexpected result it’s because you are giving it the wrong input.
You can debug it in a lot of ways, for example log the text variable in the console to see what you are working with.

1 Like

Thanks @dorilama! Got it working. Appreciate the help

1 Like