# Formatting of variable insertion causing issues with custom JS

**URL:** <https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208>\
**Category:** Ask us anything\
**Created:** [June 26, 2023, 12:17pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208 "2023-06-26T12:17:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://community.weweb.io/u/Amos)\
**Post date:** [June 26, 2023, 12:17pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/1 "2023-06-26T12:17:48Z")

</div>

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://…”

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/2/2f6b3daed93c9dac41e1de51c40fa12789770b61.png)

```auto
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”.

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 26, 2023, 12:26pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/2 "2023-06-26T12:26:35Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://community.weweb.io/u/Amos)\
**Post date:** [June 26, 2023, 12:35pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/3 "2023-06-26T12:35:02Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/b18c23902b8fe99dc12a52cbb8f178abe042aff3.png)

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 26, 2023, 12:51pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/4 "2023-06-26T12:51:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://community.weweb.io/u/Amos)\
**Post date:** [June 26, 2023, 3:32pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/5 "2023-06-26T15:32:49Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [June 26, 2023, 3:57pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/6 "2023-06-26T15:57:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Amos](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Amos](https://community.weweb.io/u/Amos)\
**Post date:** [June 28, 2023, 6:52pm UTC](https://community.weweb.io/t/formatting-of-variable-insertion-causing-issues-with-custom-js/3208/7 "2023-06-28T18:52:20Z")

</div>

Thanks @dorilama! Got it working. Appreciate the help
